-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesign twitter
48 lines (43 loc) · 1.24 KB
/
Design twitter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Twitter {
public:
unordered_map<int,set<int>> following;
unordered_map<int,vector<pair<int,int>>> mp;
int time;
Twitter() {
time=0;
}
void postTweet(int userId, int tweetId) {
mp[userId].push_back({time++,tweetId});
}
vector<int> getNewsFeed(int userId) {
priority_queue<pair<int,int>> pq;
for(int i=0;i<mp[userId].size();i++){
pq.push(mp[userId][i]);
}
for(auto j: following[userId]){
for(int i=0;i<mp[j].size();i++){
pq.push(mp[j][i]);
}
}
vector<int> ans;
for(int i=0;i<10 && !pq.empty();i++){
ans.push_back(pq.top().second);
pq.pop();
}
return ans;
}
void follow(int followerId, int followeeId) {
following[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
following[followerId].erase(followeeId);
}
};
/**
* 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);
*/