-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path355.cpp
66 lines (59 loc) · 2.12 KB
/
355.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
typedef pair<int, int> P;
class Twitter {
public:
unordered_map<int, set<P, greater<P>>> id2posts; // id->{time, tweetId}
unordered_map<int, unordered_set<int>> id2followees;
int time = 0;
Twitter() {
}
void postTweet(int userId, int tweetId) {
id2posts[userId].insert(make_pair(time, tweetId));
time++;
}
vector<int> getNewsFeed(int userId) {
if (id2followees[userId].find(userId) == id2followees[userId].end()) id2followees[userId].insert(userId);
vector<int> following;
vector<set<P, greater<P>>::iterator> currIts;
for (auto e : id2followees[userId]) {
following.push_back(e);
currIts.push_back(id2posts[e].begin());
}
int n = currIts.size();
priority_queue<pair<int, pair<int, int>>> pq;
for (int i = 0; i < n; ++i) {
if (currIts[i] == id2posts[following[i]].end()) continue;
auto [time, tweetId] = *(currIts[i]);
pq.push(make_pair(time, make_pair(tweetId, i)));
currIts[i] = next(currIts[i]);
}
int count = 10;
vector<int> res;
while (count && !pq.empty()) {
auto [time, element] = pq.top();
auto [tweetId, i] = element;
pq.pop();
res.push_back(tweetId);
if (currIts[i] != id2posts[following[i]].end()) {
auto [nextTime, nextTweetId] = *(currIts[i]);
pq.push(make_pair(nextTime, make_pair(nextTweetId, i)));
currIts[i] = next(currIts[i]);
}
count--;
}
return res;
}
void follow(int followerId, int followeeId) {
id2followees[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
id2followees[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);
*/