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);
Analysis:
1. Getnewsfeed () should be quick. We can store a user's tweets in a list, then in the "This" function, we find out all followed users and essentially Do a merges on the most recent tweets of these users (including @userId himself).
To make this quick, we can make Tweet class as a link list. So a priorityqueue<tweet> should is enough to achieve all the work.
Solution:
1 Public classTwitter {2 Private Static intTimestamp = 0;3 4 Public classTweet {5 intTweetid;6 intuserId;7 intTime ;8 Tweet Next;9 Ten PublicTweets (intUidinttid) { OneTweetid =Tid; AUserId =uid; -Time = timestamp++; -Next =NULL; the } - - } - + /**Initialize your data structure here.*/ - PublicTwitter () { +Tweetmap =NewHashmap<integer,tweet>(); AFollowmap =NewHashmap<integer,hashset<integer>>(); at - } - - /**Compose a new tweet.*/ - Public voidPosttweet (intUseridintTweetid) { -Tweet T =NewTweet (userid,tweetid); inT.next =Tweetmap.get (userId); - Tweetmap.put (userid,t); to } + - /**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. */ the PublicList<integer> Getnewsfeed (intuserId) { *List<integer> reslist =NewLinkedlist<integer>(); $priorityqueue<tweet> q =NewPriorityqueue<tweet> (A, B) (B.time-a.time));Panax Notoginseng - follow (Userid,userid); theHashset<integer> Followset =Followmap.get (userId); + if(Followset! =NULL){ A for(Integer id:followset) the if(Tweetmap.containskey (ID)) Q.add (Tweetmap.get (id)); + } - $ while(Reslist.size () <10 &&!Q.isempty ()) { $Tweet T =Q.poll (); - Reslist.add (t.tweetid); - if(t.next!=NULL) Q.add (t.next); the } - Wuyi the returnreslist; - } Wu - /**Follower follows a followee. If The operation is invalid, it should be a no-op.*/ About Public voidFollow (intFollowerid,intFolloweeid) { $ if(Followmap.containskey (Followerid)) { - Followmap.get (Followerid). Add (Followeeid); -}Else { -Hashset<integer> set =NewHashset<integer>(); A Set.add (Followeeid); + Followmap.put (followerid,set); the } - } $ the /**Follower unfollows a followee. If The operation is invalid, it should be a no-op.*/ the Public voidUnfollow (intFollowerid,intFolloweeid) { the if(Followmap.containskey (Followerid)) { the Followmap.get (Followerid). Remove (Followeeid); - } in } the theHashmap<integer,tweet>Tweetmap; AboutHashmap<integer,hashset<integer>>Followmap; the } the the /** + * Your Twitter object would be instantiated and called as such: - * Twitter obj = new Twitter (); the * Obj.posttweet (Userid,tweetid);Bayi * list<integer> param_2 = obj.getnewsfeed (userId); the * Obj.follow (Followerid,followeeid); the * Obj.unfollow (Followerid,followeeid); - */
Leetcode-design Twitter