-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesign_Twitter.cpp
109 lines (87 loc) · 3.17 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <set>
#include <map>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
// Better way to keep a vector of all tweets and iterate starting from the end, seeing if the tweet
// is from a followee
struct user {
int userId;
std::unordered_set<int> follows; // The users that this user follows
std::vector<std::pair<int, int>> tweets; // The tweets that a user has created along
// w/ their timestamp
user(int user) : userId(user), follows({user}){}
};
class Twitter {
private:
std::unordered_map<int, user*> users; // keep track of all users, their followers and tweets
int timeStep;
public:
Twitter() {
timeStep = 0;
}
void postTweet(int userId, int tweetId) {
// Create user if not present
if (users.find(userId) == users.end()) {
users[userId] = new user(userId);
}
users[userId]->tweets.push_back({tweetId, timeStep});
timeStep++;
return;
}
std::vector<int> getNewsFeed(int userId) {
// Merge all the tweets of the users that the userId follows
if (users.find(userId) == users.end()) {
return {};
}
std::unordered_set<int> followedUsers = users[userId]->follows;
std::vector<int> tweets;
std::vector<std::pair<int, int>> allTweets;
for (auto i=followedUsers.begin(); i!=followedUsers.end(); i++) {
allTweets.insert(allTweets.end(), users[*i]->tweets.begin(), users[*i]->tweets.end());
}
// sort based on timestamp
sort(allTweets.begin(), allTweets.end(), [](std::pair<int, int> a, std::pair<int, int> b) {
return a.second > b.second;
});
for (int i=0; i<allTweets.size(); i++) {
tweets.push_back(allTweets[i].first);
}
int maxIndex = tweets.size() >= 10 ? 10 : tweets.size();
std::vector<int> answer(tweets.begin(), tweets.begin() + maxIndex);
return answer;
}
void follow(int followerId, int followeeId) {
// Add followee to follower's set
// create users if not yet existent
if (users.find(followerId) == users.end()) {
users[followerId] = new user(followerId);
}
if (users.find(followeeId) == users.end()) {
users[followeeId] = new user(followeeId);
}
// Add new folowee
users[followerId]->follows.insert(followeeId);
return;
}
void unfollow(int followerId, int followeeId) {
// Remove followee
// create users if not yet existent
if (users.find(followerId) == users.end()) {
users[followerId] = new user(followerId);
}
if (users.find(followeeId) == users.end()) {
users[followeeId] = new user(followeeId);
}
users[followerId]->follows.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);
*/