-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (120 loc) · 4.83 KB
/
main.cpp
File metadata and controls
137 lines (120 loc) · 4.83 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
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
134
135
136
137
// Source: https://leetcode.com/problems/design-twitter
// Title: Design Twitter
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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.
//
// Implement the `Twitter` class:
//
// - `Twitter()` Initializes your twitter object.
// - `void postTweet(int userId, int tweetId)` Composes a new tweet with ID `tweetId` by the user `userId`. Each call to this function will be made with a unique `tweetId`.
// - `List<Integer> getNewsFeed(int userId)` Retrieves 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 themself. Tweets must be **ordered from most recent to least recent**.
// - `void follow(int followerId, int followeeId)` The user with ID `followerId` started following the user with ID `followeeId`.
// - `void unfollow(int followerId, int followeeId)` The user with ID `followerId` started unfollowing the user with ID `followeeId`.
//
// **Example 1:**
//
// ```
// Input:
// ["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"]
// [[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
//
// Output:
// [null, null, [5], null, null, [6, 5], null, [5]]
//
// Explanation:
// Twitter twitter = new Twitter();
// twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5).
// twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]. return [5]
// twitter.follow(1, 2); // User 1 follows user 2.
// twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6).
// twitter.getNewsFeed(1); // 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.unfollow(1, 2); // User 1 unfollows user 2.
// twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2.
// ```
//
// **Constraints:**
//
// - `1 <= userId, followerId, followeeId <= 500`
// - `0 <= tweetId <= 10^4`
// - All the tweets have **unique** IDs.
// - At most `3 * 10^4` calls will be made to `postTweet`, `getNewsFeed`, `follow`, and `unfollow`.
// - A user cannot follow himself.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <array>
#include <deque>
#include <queue>
#include <unordered_set>
#include <vector>
using namespace std;
// Fanout-on-read (construct feed on read)
//
// Use array of hash set for follow relationship.
// Use deque for user tweet (newest to front).
// For news feed, loop through all its followee, and merge the tweets using a heap.
class Twitter {
constexpr static int kMaxUser = 500;
constexpr static int kMaxTweet = 10;
struct Tweet {
int id;
int time;
};
using Queue = deque<Tweet>;
using QueueIt = Queue::const_iterator;
struct Node {
QueueIt curr; // current iterator
QueueIt end; // end iterator
bool isEnd() const { return curr == end; }
Node& operator++() {
++curr;
return *this;
}
};
struct NodeComp {
bool operator()(const Node& a, const Node& b) { return a.curr->time < b.curr->time; };
};
using Heap = priority_queue<Node, vector<Node>, NodeComp>; // max-heap by time
array<unordered_set<int>, kMaxUser + 1> following; // follower -> followee
array<Queue, kMaxUser + 1> tweets; // user -> tweet
int now = 0;
public:
Twitter() {
for (int u = 1; u <= kMaxUser; ++u) {
following[u].insert(u); // follow self
}
}
void postTweet(int userId, int tweetId) {
if (tweets[userId].size() == kMaxTweet) tweets[userId].pop_back();
tweets[userId].push_front({.id = tweetId, .time = ++now});
}
vector<int> getNewsFeed(int userId) {
// Init heap
Heap heap;
for (const int followeeId : following[userId]) {
Node node{tweets[followeeId].cbegin(), tweets[followeeId].cend()};
if (!node.isEnd()) heap.push(node);
}
// Construct feed
auto feed = vector<int>();
feed.reserve(kMaxTweet);
while (!heap.empty() && feed.size() < kMaxTweet) {
Node node = heap.top();
heap.pop();
feed.push_back(node.curr->id);
// Next node
++node;
if (!node.isEnd()) heap.push(node);
}
return feed;
}
void follow(int followerId, int followeeId) {
if (followerId == followeeId) return;
following[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
if (followerId == followeeId) return;
following[followerId].erase(followeeId);
}
};