Implement a simple Twitter. Support the following method:
Posttweet (user_id, Tweet_text). Post a tweet.
Gettimeline (user_id). Get the given user's most recently tweets posted by himself, order by timestamp from most recent to least recent.
Getnewsfeed (user_id). Get the given user's most recently tweets in his news feed (posted by his friends and himself). Order by timestamp from the most recent to least recent.
Follow (from_user_id, to_user_id). FROM_USER_ID followed to_user_id.
Unfollow (from_user_id, to_user_id). from_user_id unfollowed to to_user_id.
Example
Posttweet (1, "Lintcode is good!!!")
>> 1
Getnewsfeed (1)
>> [1]
Gettimeline (1)
>> [1]
Follow (2, 1)
Getnewsfeed (2)
>> [1]
Unfollow (2, 1)
Getnewsfeed (2)
>> []
This problem allows us to implement a mini tweet, with release messages, get timeline, novelty, add attention and cancel attention to features such as getting the user's timeline is back to the latest 10 tweets, while the novelty is to return the latest 10 of their own and friends of Twitter, if the attention is canceled, Then the new news of the return has no cancellation of the attention of the friend's Twitter. This is a very interesting design problem, we do not really go to get the system time to sort the tweets in order to simplify the question, but we use a variable order, each post a message, order Increment 1, so we know the order of the release of the late, we create a new struct node, To bind an order to each tweet, and then we write a function that returns the last 10 node from a node array, and a function that returns the first 10 node from the node array, and then we need two hash tables, One is used to establish a mapping between each user and all of his friends, and the other is to establish a mapping between each user and all tweets it publishes, and we need a variable order to record the order in which the tweets are published.
For the Posttweet function, we first use the CREATE function provided by the tweet class to create a tweet, and then we see if the publisher is in Users_tweets, if it is not added to the user, and then pushes the Dobby into the array and its mapping, and finally returns the tweet.
For the Gettimeline function, we will first return the latest 10 tweets from the user's Twitter set, then sort them chronologically and then return.
For the Getnewsfeed function, we first save the user's latest 10 in the Twitter set, then traverse all of their friends, save the latest 10 of their friends, and then sort the entire chronological order, returning to the latest 10.
For the follow function, we add friends to the user's buddy table.
For the Unfollow function, we remove a friend from the user's buddy table.
classMinitwitter { Public: structNode {intorder; Tweet tweets; Node (into, tweet T): Order (O), tweet (t) {}}; Vector<Node> Getlastten (vector<node>t) {intLast =Ten; if(T.size () <Ten) Last =t.size (); returnVector<node> (T.end ()-Last , T.end ()); } Vector<Node> Getfirstten (vector<node>t) {intLast =Ten; if(T.size () <Ten) Last =t.size (); returnVector<node> (T.begin (), T.begin () +Last ); } minitwitter () {Order=0; } //@param user_id an integer//@param tweet a string//return a tweetTweet Posttweet (intUSER_ID,stringTweet_text) {Tweet Tweet=tweet::create (user_id, Tweet_text); if(!users_tweets.count (USER_ID)) users_tweets[user_id] = {}; ++order; Users_tweets[user_id].push_back (Node (order, tweet)); returntweets; } //@param user_id an integer//return a list of ten new feeds recently//and Sort by timelineVector<tweet> Getnewsfeed (intuser_id) {Vector<Node>T; if(Users_tweets.count (user_id)) {T=Getlastten (users_tweets[user_id]); } if(Friends.count (user_id)) { for(Auto it:friends[user_id]) {if(Users_tweets.count (it)) {vector<Node> v =Getlastten (Users_tweets[it]); T.insert (T.end (), V.begin (), V.end ()); }}} sort (T.begin (), T.end (), [] (ConstNode &a,ConstNode &b) {returnA.order >B.order;}); Vector<Tweet>Res; T=Getfirstten (t); for(Auto a:t) {res.push_back (a.tweet); } returnRes; } //@param user_id an integer//return a list of ten new posts recently//and Sort by timelineVector<tweet> Gettimeline (intuser_id) {Vector<Node>T; if(Users_tweets.count (user_id)) {T=Getlastten (users_tweets[user_id]); } sort (T.begin (), T.end (), [] (ConstNode &a,ConstNode &b) {returnA.order >B.order;}); Vector<Tweet>Res; T=Getfirstten (t); for(Auto a:t) {res.push_back (a.tweet); } returnRes; } //@param from_user_id an integer//@param to_user_id an integer//From user_id follows to_user_id voidFollow (intFROM_USER_ID,intto_user_id) {Friends[from_user_id].insert (to_user_id); } //@param from_user_id an integer//@param to_user_id an integer//From user_id unfollows to_user_id voidUnfollow (intFROM_USER_ID,intto_user_id) {friends[from_user_id].erase (to_user_id); }Private: Unordered_map<int,Set<int>>friends; Unordered_map<int, vector<node>>users_tweets; intorder;};
Resources:
http://www.jiuzhang.com/solutions/mini-twitter/
[Lintcode] mini Twitter mini tweet