-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path0355-design-twitter.cpp
95 lines (79 loc) · 4.26 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
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
/*
Problem: LeetCode 355 - Design Twitter
Description:
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and see the 10 most recent tweets in the user's news feed.
Intuition:
To design Twitter, we need to efficiently handle the following operations:
1. Posting a tweet: We need to store the tweets along with their timestamps.
2. Following/Unfollowing a user: We need to maintain a data structure to track the followers and followees of each user.
3. Retrieving the news feed: We need to combine the tweets from the user's own timeline along with the tweets from the users they follow.
Approach:
1. Implement the `User` class to store the user's information, including their tweets and the users they follow.
2. Use an unordered_map to store the user ID as the key and the corresponding `User` object as the value.
3. Implement the `Tweet` class to store the tweet's information, including the tweet ID and the timestamp.
4. Use a deque to store the tweets in the user's timeline, with the most recent tweet at the front.
5. To post a tweet:
- Get the `User` object corresponding to the user ID.
- Create a new `Tweet` object with the tweet ID and the current timestamp.
- Add the tweet to the user's timeline and update the timestamp.
6. To follow a user:
- Get the `User` objects corresponding to the follower and followee IDs.
- Add the followee ID to the follower's list of followees.
7. To unfollow a user:
- Get the `User` objects corresponding to the follower and followee IDs.
- Remove the followee ID from the follower's list of followees.
8. To retrieve the news feed:
- Get the `User` object corresponding to the user ID.
- Create a priority_queue to store the tweets from the user's timeline and the timelines of the users they follow.
- Iterate over the tweets in the user's timeline and add them to the priority queue.
- Iterate over the followees of the user and add their tweets to the priority queue.
- Pop the top 10 tweets from the priority queue and return them in reverse order.
Time Complexity:
The time complexity of posting a tweet, following/unfollowing a user, and retrieving the news feed is O(log n), where n is the number of tweets. This is because we use a priority queue to retrieve the top 10 tweets in the news feed.
Space Complexity:
The space complexity is O(m + n), where m is the number of users and n is the number of tweets. We store the user information in the unordered_map and the tweets in the deque.
*/
class Twitter {
private:
struct Tweet {
int tweetId;
int timestamp;
Tweet(int id, int time) : tweetId(id), timestamp(time) {}
};
unordered_map<int, vector<Tweet>> userTweets; // Store tweets of each user
unordered_map<int, unordered_set<int>> userFollowees; // Store followees of each user
int time; // Keep track of the timestamp
public:
Twitter() {
time = 0;
}
void postTweet(int userId, int tweetId) {
userTweets[userId].emplace_back(tweetId, time++); // Add the tweet with the current timestamp
}
void follow(int followerId, int followeeId) {
userFollowees[followerId].insert(followeeId); // Add followee to the follower's set of followees
}
void unfollow(int followerId, int followeeId) {
userFollowees[followerId].erase(followeeId); // Remove followee from the follower's set of followees
}
vector<int> getNewsFeed(int userId) {
vector<int> newsFeed;
priority_queue<pair<int, int>> pq; // Use a priority queue to sort tweets by timestamp (max-heap)
// Add the user's own tweets to the priority queue
for (const auto &tweet : userTweets[userId]) {
pq.push({ tweet.timestamp, tweet.tweetId });
}
// Add tweets from the user's followees to the priority queue
for (int followeeId : userFollowees[userId]) {
for (const auto &tweet : userTweets[followeeId]) {
pq.push({ tweet.timestamp, tweet.tweetId });
}
}
// Retrieve the top 10 tweets from the priority queue
while (!pq.empty() && newsFeed.size() < 10) {
newsFeed.push_back(pq.top().second); // Add the tweet ID to the news feed
pq.pop();
}
return newsFeed;
}
};