-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path355.design-twitter.cpp
134 lines (127 loc) · 4.3 KB
/
355.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* @lc app=leetcode id=355 lang=cpp
*
* [355] Design Twitter
*
* https://leetcode.com/problems/design-twitter/description/
*
* algorithms
* Medium (26.98%)
* Total Accepted: 32.6K
* Total Submissions: 120.8K
* Testcase Example: '["Twitter","postTweet","getNewsFeed","follow","postTweet","getNewsFeed","unfollow","getNewsFeed"]\n[[],[1,5],[1],[1,2],[2,6],[1],[1,2],[1]]'
*
* 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. Your design should support the following methods:
*
*
*
* postTweet(userId, tweetId): Compose a new tweet.
* getNewsFeed(userId): 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.
* follow(followerId, followeeId): Follower follows a followee.
* unfollow(followerId, followeeId): Follower unfollows a followee.
*
*
*
* Example:
*
* Twitter twitter = new Twitter();
*
* // User 1 posts a new tweet (id = 5).
* twitter.postTweet(1, 5);
*
* // User 1's news feed should return a list with 1 tweet id -> [5].
* twitter.getNewsFeed(1);
*
* // User 1 follows user 2.
* twitter.follow(1, 2);
*
* // User 2 posts a new tweet (id = 6).
* twitter.postTweet(2, 6);
*
* // User 1's news feed should return a list with 2 tweet ids -> [6, 5].
* // Tweet id 6 should precede tweet id 5 because it is posted after tweet id
* 5.
* twitter.getNewsFeed(1);
*
* // User 1 unfollows user 2.
* twitter.unfollow(1, 2);
*
* // User 1's news feed should return a list with 1 tweet id -> [5],
* // since user 1 is no longer following user 2.
* twitter.getNewsFeed(1);
*
*
*/
class Twitter {
public:
#define USER_ID int
#define TWEET_ID int
struct TweetUser{
TWEET_ID tweetId;
USER_ID userId;
TweetUser(TWEET_ID t, USER_ID u) :tweetId(t), userId(u) {}
};
map<USER_ID,set<USER_ID>> follower_followee;
vector<TweetUser> tweets;
/** Initialize your data structure here. */
Twitter() {
}
/** Compose a new tweet. */
void postTweet(USER_ID userId, TWEET_ID tweetId) {
tweets.push_back(TweetUser(tweetId, userId));
}
/** 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<TWEET_ID> getNewsFeed(USER_ID userId) {
auto userItor = follower_followee.find(userId);
set<USER_ID>* followees = nullptr;
if(userItor != follower_followee.end()){
followees = &userItor->second;
}
vector<TWEET_ID> feeds;
int count = 0;
for(auto it = tweets.rbegin(); it != tweets.rend() && count < 10; ++it){
USER_ID followeeId = it->userId;
if(it->userId == userId ||
(followees && followees->find(followeeId) != followees->end())){
feeds.push_back(it->tweetId);
++count;
}
}
return feeds;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(USER_ID followerId, USER_ID followeeId) {
if(followerId == followeeId){
return;
}
follower_followee[followerId].insert(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(USER_ID followerId, USER_ID followeeId) {
if(followerId == followeeId){
return;
}
auto it = follower_followee.find(followerId);
if(it == follower_followee.end()){
return;
}
set<USER_ID>& followees = it->second;
auto found = find(followees.begin(), followees.end(), followeeId);
if(found != followees.end()){
followees.erase(found);
}
}
};
/**
* 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);
*/