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);
Public classTwitter {intTimeStamp = 0; Map<integer, user>map; classTweet {intID; intTime ; Tweet Next; PublicTweets (intID) { This. ID =ID; time= timestamp++; Next=NULL; } } classUser {intID; Set<Integer>followed; Tweet Tweet_head; PublicUser (intID) { This. ID =ID; Followed=NewHashset<>(); Followed.add (ID); Tweet_head=NULL; } Public voidFollow (intID) {Followed.add (ID); } Public voidUnfollow (intID) {followed.remove (ID); } Public voidPostintID) {Tweet T=NewTweet (ID); T.next=Tweet_head; Tweet_head=T; } } /**Initialize your data structure here.*/ PublicTwitter () {map=NewHashmap<>(); } /**Compose a new tweet.*/ Public voidPosttweet (intUseridintTweetid) { if(!Map.containskey (userId)) {User User=NewUser (userId); Map.put (userId, user); } map.get (UserId). Post (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. */ PublicList<integer> Getnewsfeed (intuserId) {List<Integer> res =NewArraylist<>(); if(!map.containskey (USERID))returnRes; Priorityqueue<Tweet> PQ =NewPriorityqueue<tweet> (NewComparator<tweet>(){ Public intCompare (tweet A, tweet b) {returnB.time-A.time; } }); for(The Integer user:map.get (userId). followed) {Tweet T=map.get (user). Tweet_head; if(t! =NULL) {Pq.add (t); } } intn = 0; while(!pq.isempty () && N < 10) {Tweet T=Pq.poll (); Res.add (t.id); N++; if(T.next! =NULL) Pq.add (T.next); } returnRes; } /**Follower follows a followee. If The operation is invalid, it should be a no-op.*/ Public voidFollow (intFollowerid,intFolloweeid) { if(!Map.containskey (Followerid)) {User User=NewUser (Followerid); Map.put (Followerid, user); } if(!Map.containskey (Followeeid)) {User User=NewUser (Followeeid); Map.put (Followeeid, user); } map.get (Followerid). Follow (Followeeid); } /**Follower unfollows a followee. If The operation is invalid, it should be a no-op.*/ Public voidUnfollow (intFollowerid,intFolloweeid) { if(!map.containskey (followerid) | |!map.containskey (followeeid) | | followerid = = Followeeid)return ; Map.get (Followerid). Unfollow (Followeeid); }}/*** Your Twitter object would be instantiated and called as such: * twitter obj = new Twitter (); * Obj.posttweet (UserId, Tweetid); * List<integer> param_2 = obj.getnewsfeed (userId); * Obj.follow (Followerid,followeeid); * Obj.unfollow (Followerid,followeeid); */
355. Design Twitter [Classic Design]