forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path355. Design Twitter.cpp
More file actions
48 lines (43 loc) · 1.24 KB
/
355. Design Twitter.cpp
File metadata and controls
48 lines (43 loc) · 1.24 KB
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 {
private:
int time;
unordered_map<int, unordered_set<int>> mp; // Stores followers
unordered_map<int, vector<pair<int, int>>> mp2; // Stores tweets (time, tweetId)
public:
Twitter() {
time=0;
}
void postTweet(int userId, int tweetId) {
mp2[userId].emplace_back(time,tweetId);
time++;
}
vector<int> getNewsFeed(int userId) {
priority_queue<pair<int,int>> pq;
for(auto i: mp2[userId]) pq.push(i);
for(auto i: mp[userId]){
for(auto j: mp2[i]) pq.push(j);
}
vector<int> feed;
int count=10;
while(!pq.empty() && count){
feed.push_back(pq.top().second);
pq.pop();
count--;
}
return feed;
}
void follow(int followerId, int followeeId) {
mp[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
mp[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);
*/