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 most recent tweets IDs in the user ' s news Feed. Each item in the news feed must is posted by the users of the user followed or by the user herself. Tweets must is ordered from the most recent to least recent.
- Follow (Followerid, Followeeid): Follower follows a followee.
- unfollow (Followerid, Followeeid): Follower unfollows a followee.
Example:
twitter Twitter = new Twitter ()//user 1 posts a new tweet (id = 5). Twitter.posttweet (1, 5);//user 1 ' s News Feed should Return a list with 1 tweet ID, [5].twitter.getnewsfeed (1);//user 1 follows user 2.twitter.follow (1, 2);//USER 2 pos TS a new tweet (id = 6). Twitter.posttweet (2, 6);//User 1 's News Feed should return a list with 2 tweet IDs, [6, 5]./ /Tweet ID 6 should precede tweet ID 5 because it is posted after Tweet ID 5.twitter.getnewsfeed (1);/User 1 unfollows US Er 2.twitter.unfollow (1, 2);//User 1 's News Feed should return a list with 1 tweet ID--[5],//since User 1 is no lon Ger following user 2.twitter.getnewsfeed (1);
This topic allows us to design a simple tweet with the ability to post messages, get new, add attention and remove concerns. We need to do two hash tables, the first one is to build a mapping between the user and all of his friends, and the other is to create a mapping between the user and all of its messages. Since it is time-ordered to get something new, we can use an integer variable CNT to simulate the point in time, with each message, cnt increasing by 1, then we know that CNT is the most recent. We also need to establish a mapping between each message and its point-in-time CNT when we build a mapping between the user and all of its messages. The main difficulty of this problem is to implement the Getnewsfeed () function, which gets the last 10 messages of itself and friends, and we do this by adding the user to their friends list, and then traversing all of the user's friends, traversing all the messages of each friend, and maintaining a hash table of size 10. If the newly traversed message is later than the oldest message in the hash table, then add the message and delete the oldest message so that we can find the last 10 messages, see the code below:
Solution One:
classTwitter { Public: /** Initialize your data structure here.*/Twitter () {CNT=0; } /** Compose a new tweet.*/ voidPosttweet (intUseridintTweetid) {Follow (userid, UserID); Tweets[userid].insert ({cnt++, Tweetid}); } /** Retrieve The most recent tweets IDs in the user s news feed. Each item in the news feed must is posted by the users of the user followed or by the user herself. Tweets must is ordered from the most recent to least recent. */Vector<int> Getnewsfeed (intuserId) {Vector<int>Res; Map<int,int>Top10; for(Auto it = Friends[userid].begin (); It! = Friends[userid].end (); + +it) { intt = *it; for(Auto a = Tweets[t].begin (); A! = Tweets[t].end (); + +a) {if(Top10.size () >0&& top10.begin ()->first > A->first && top10.size () >Ten) Break; Top10.insert ({a->first, a->Second}); if(Top10.size () >Ten) Top10.erase (Top10.begin ()); } } for(Auto it = Top10.rbegin (); It! = Top10.rend (); + +it) {Res.push_back (It-second); } returnRes; } /** Follower follows a followee. If The operation is invalid, it should be a no-op.*/ voidFollow (intFollowerid,intFolloweeid) {Friends[followerid].insert (Followeeid); } /** Follower unfollows a followee. If The operation is invalid, it should be a no-op.*/ voidUnfollow (intFollowerid,intFolloweeid) { if(Followerid! =Followeeid) {friends[followerid].erase (Followeeid); } } Private: intCNT; Unordered_map<int,Set<int>>friends; Unordered_map<int, map<int,int>>tweets;};
The following method is the same as the above basic, is to save the user all the message, the use of Vector<pair<int, int>>, so we can use Priority_queue to help us find the latest 10 messages, see the code is as follows:
Solution Two:
classTwitter { Public: /** Initialize your data structure here.*/Twitter () {CNT=0; } /** Compose a new tweet.*/ voidPosttweet (intUseridintTweetid) {Follow (userid, UserID); Tweets[userid].push_back ({cnt++, Tweetid}); } /** Retrieve The most recent tweets IDs in the user s news feed. Each item in the news feed must is posted by the users of the user followed or by the user herself. Tweets must is ordered from the most recent to least recent. */Vector<int> Getnewsfeed (intuserId) {Vector<int>Res; Priority_queue<pair<int,int, vector<pair<int,int>>, greater<pair<int,int>>>Q; for(Auto it = Friends[userid].begin (); It! = Friends[userid].end (); + +it) { for(Auto a = Tweets[*it].begin (); A! = Tweets[*it].end (); + +a) {if(Q.size () >0&& Q.top (). First > A->first && q.size () >Ten) Break; Q.push (*a); if(Q.size () >Ten) Q.pop (); } } while(!Q.empty ()) {Res.push_back (Q.top (). second); Q.pop (); } reverse (Res.begin (), Res.end ()); returnRes; } /** Follower follows a followee. If The operation is invalid, it should be a no-op.*/ voidFollow (intFollowerid,intFolloweeid) {Friends[followerid].insert (Followeeid); } /** Follower unfollows a followee. If The operation is invalid, it should be a no-op.*/ voidUnfollow (intFollowerid,intFolloweeid) { if(Followerid! =Followeeid) {friends[followerid].erase (Followeeid); } } Private: intCNT; Unordered_map<int,Set<int>>friends; Unordered_map<int, vector<pair<int,int>>>tweets;};
Resources:
Https://leetcode.com/discuss/107612/my-wa-c-code
Https://leetcode.com/discuss/107637/short-c-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Design Twitter