355. Design Twitter

来源:互联网 发布:vb二级考试题库 编辑:程序博客网 时间:2024/05/16 11:49

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods:

postTweet(userId, tweetId): Compose a new tweet.
getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
follow(followerId, followeeId): Follower follows a followee.
unfollow(followerId, followeeId): Follower unfollows a followee.

class Twitter {private:    unordered_map<int,set<int>>followee;    unordered_map<int, vector<pair<int, int>>>tweet;    int count;public:    /** Initialize your data structure here. */    Twitter() {        count = 0;    }    /** Compose a new tweet. */    void postTweet(int userId, int tweetId) {        tweet[userId].push_back({count++, tweetId});    }    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */    vector<int> getNewsFeed(int userId) {        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> sorttweets;        followee[userId].insert(userId);        for(set<int>::iterator it=followee[userId].begin(); it!=followee[userId].end();++it)            for(vector<pair<int,int>>::reverse_iterator rit=tweet[*it].rbegin(); rit!=tweet[*it].rend(); ++rit)            {                if(sorttweets.empty())                {                    sorttweets.push(*rit);                }                else                {                    if(rit->first < sorttweets.top().first && sorttweets.size() == 10 )                    {                        break;                    }                    sorttweets.push(*rit);                    if(sorttweets.size()>10)                    {                        sorttweets.pop();                    }                }            }        vector<int> res;        while(!sorttweets.empty())        {            res.push_back(sorttweets.top().second);            sorttweets.pop();        }        reverse(res.begin(), res.end());        followee[userId].erase(userId);        return res;    }    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */    void follow(int followerId, int followeeId) {        if(followerId != followeeId)        {            followee[followerId].insert(followeeId);        }    }    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */    void unfollow(int followerId, int followeeId) {        if (followerId != followeeId) {            followee[followerId].erase(followeeId);        }    }};/** * Your Twitter object will be instantiated and called as such: * Twitter obj = new Twitter(); * obj.postTweet(userId,tweetId); * vector<int> param_2 = obj.getNewsFeed(userId); * obj.follow(followerId,followeeId); * obj.unfollow(followerId,followeeId); */
0 0
原创粉丝点击