-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignTwitter.cpp
More file actions
63 lines (55 loc) · 2.14 KB
/
designTwitter.cpp
File metadata and controls
63 lines (55 loc) · 2.14 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Source: https://leetcode.com/problems/design-twitter/
// Author: Miao Zhang
// Date: 2021-02-04
const size_t MaxTweets = 10;
class Twitter {
public:
/** Initialize your data structure here. */
Twitter() {
}
/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
auto& tweets = user_tweets_[userId];
if (tweets.size() == MaxTweets) {
tweets.pop_front();
}
tweets.emplace_back(++time, tweetId);
}
/** Retrieve 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 herself. Tweets must be ordered from most recent to least recent. */
vector<int> getNewsFeed(int userId) {
vector<pair<int, int>> feed(begin(user_tweets_[userId]), end(user_tweets_[userId]));
for (int uid: user_followers_[userId]) {
feed.insert(end(feed),
begin(user_tweets_[uid]),
end(user_tweets_[uid]));
}
sort(rbegin(feed), rend(feed));
vector<int> res;
for (int i = 0; i < min(MaxTweets, feed.size()); i++) {
res.push_back(feed[i].second);
}
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
if (followerId == followeeId) return;
user_followers_[followerId].insert(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
if (followerId == followeeId) return;
user_followers_[followerId].erase(followeeId);
}
private:
int time;
unordered_map<int, deque<pair<int, int>>> user_tweets_;
unordered_map<int, set<int>> user_followers_;
};
/**
* 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);
*/