-
Notifications
You must be signed in to change notification settings - Fork 0
/
Design Twitter.cpp
94 lines (76 loc) · 3.18 KB
/
Design Twitter.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Twitter {
private:
int time;
int maxFeed;
unordered_map<int, deque<pair<int,int>> > tweets;
// user id time tweetid
unordered_map<int, list<int> > following;
// user followee
unordered_map<long, list<int>::iterator > follower_followee;
long getKey(int followerId, int followeeId) {
return (long)followerId << 32 | (long)followeeId;
}
public:
Twitter() {
time = 0;
maxFeed = 10;
}
void postTweet(int userId, int tweetId) {
tweets[userId].push_back({time++, tweetId});
if(tweets[userId].size() > maxFeed) {
tweets[userId].pop_front();
}
}
vector<int> getNewsFeed(int userId) {
vector<int> res;
priority_queue<pair<int,int>> pq;
for(auto tweet: tweets[userId]) {
pq.push(tweet);
}
for(auto followee: following[userId]) {
for(auto tweet: tweets[followee]) {
pq.push(tweet);
}
}
while(!pq.empty()) {
if(res.size() == maxFeed)
break;
res.push_back(pq.top().second);
pq.pop();
}
return res;
}
void follow(int followerId, int followeeId) {
if(followerId != followeeId) {
if(follower_followee.find(getKey(followerId,followeeId)) == follower_followee.end()) {
// i.e; the relationship already doesn't exist
following[followerId].push_front(followeeId);
auto key = getKey(followerId, followeeId);
follower_followee[key] = following[followerId].begin();
}
}
}
void unfollow(int followerId, int followeeId) {
auto key = getKey(followerId, followeeId);
if(follower_followee.find(key) != follower_followee.end()){
following[followerId].erase(follower_followee[key]);
follower_followee.erase(key);
}
}
};
/**
* 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);
*/
// 355 LEETCODE
// 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.
// Implement the Twitter class:
// Twitter() Initializes your twitter object.
// void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user userId. Each call to this function will be made with a unique tweetId.
// List<Integer> getNewsFeed(int userId) Retrieves 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 themself. Tweets must be ordered from most recent to least recent.
// void follow(int followerId, int followeeId) The user with ID followerId started following the user with ID followeeId.
// void unfollow(int followerId, int followeeId) The user with ID followerId started unfollowing the user with ID followeeId.