-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0355-design-twitter.cpp
65 lines (53 loc) · 1.76 KB
/
0355-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
/*
Design Twitter: post tweets, follow/unfollow, see recent tweets
Maintain user -> tweet pairs & hash map {user -> ppl they follow}
Time: O(n)
Space: O(n)
*/
class Twitter {
public:
Twitter() {
}
void postTweet(int userId, int tweetId) {
posts.push_back({userId, tweetId});
}
vector<int> getNewsFeed(int userId) {
// 10 tweets
int count = 10;
vector<int> result;
// since postTweet pushes to the back, looping from back gets most recent
for (int i = posts.size() - 1; i >= 0; i--) {
if (count == 0) {
break;
}
int followingId = posts[i].first;
int tweetId = posts[i].second;
unordered_set<int> following = followMap[userId];
// add to result if they're following them or it's a tweet from themself
if (following.find(followingId) != following.end() || followingId == userId) {
result.push_back(tweetId);
count--;
}
}
return result;
}
void follow(int followerId, int followeeId) {
followMap[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
followMap[followerId].erase(followeeId);
}
private:
// pairs: [user, tweet]
vector<pair<int, int>> posts;
// hash map: {user -> people they follow}
unordered_map<int, unordered_set<int>> followMap;
};
/**
* 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);
*/