Design Twitter
[Problem]
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. 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 posts 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 user 2. twitter.unfollow(1, 2); // User 1‘s news feed should return a list with 1 tweet id -> [5], // since user 1 is no longer following user 2. twitter.getNewsFeed(1);
[Solution]
- define struct User: "set<uid> followees", "vector<pair<postTime, tid>> tweets"
- twitter: "map<uid, user> users"
- getNewFeed(): minHeap<pair<int postTime, int tid>> q, size k=10
1. for i=n-1~0 of cur_user.tweets //visit backward so last first
2. for each followee
for i=n-1~0 of followee.tweets //visit backward so last first
if(q.size()==10&& tweets[i].first<q.top().first) break; //q full and all left tweets are earlier, move to next user
q.push(tweets[i]);
if(q.size()>10) q.pop(); // +new-min => q size=k
3. while(!q.empty) { res.push_back(q.top()); q.pop}
4. reverse(res.begin(), res.end()) // minHeap pop in asc order => reverse to get desc order so last first
[Tip]
- top10 with maximum postTime => minHeap (size=10 to filter out smallers) //O(klogk)
- tweets.push_back(last) => visit tweets vector backward to get the latest first //O(num_user*num_tweets_per_user)
- minHeap pops content in asc order => reverse the popped results//O(klogk)
- follow(uid1, uid2): only if(uid1!=uid2) users[uid1].followees.insert(uid2)
class Twitter { public: /** Initialize your data structure here. */ Twitter() { } /** Compose a new tweet. */ void postTweet(int userId, int tweetId) { users[userId].tweets.push_back(make_pair(postTime++, 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) { // Top10 with max postTime => minHeap to filter all smallers // User‘s tweets stored order: earlier -> later, so need to visit backward //O(n*m), n=followees_cnt, m=tweets/person // MinHeap but need to sort in decending order, so need to reverse popped heap content //O(klogk), k=10 priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; // Add tweets post by userId for(int i = users[userId].tweets.size()-1; i >=0; --i) { //earlier stored first, so visit backward pair<int, int> t_pair = users[userId].tweets[i]; if(pq.size() == 10 && t_pair.first < pq.top().first) break; //q full+only earlier tweets left, skip this user pq.push(t_pair); if(pq.size() > 10) pq.pop(); } // Add tweets post by followees for(int f_id : users[userId].followees) { for(int i = users[f_id].tweets.size()-1; i >=0; --i) { //earlier stored first, so visit backward pair<int, int> t_pair = users[f_id].tweets[i]; if(pq.size() == 10 && t_pair.first < pq.top().first) break;//q full+only earlier tweets left, skip this user pq.push(t_pair); if(pq.size() > 10) pq.pop(); } } vector<int> res; while(!pq.empty()) { res.push_back(pq.top().second); pq.pop(); } reverse(res.begin(), res.end()); //minHeap pops earlier first, so need to reverse 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) { users[followerId].followees.insert(followeeId); } } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ void unfollow(int followerId, int followeeId) { users[followerId].followees.erase(followeeId); //no exception when erasing non-existing value from set } private: struct User { set<int> followees; vector<pair<int, int>> tweets; // <postTime,tid> }; unordered_map<int, User> users; int postTime = 0; }; /** * 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); */