You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/fishercoder/solutions/_355.java
+20-44
Original file line number
Diff line number
Diff line change
@@ -8,43 +8,6 @@
8
8
importjava.util.PriorityQueue;
9
9
importjava.util.Set;
10
10
11
-
/**
12
-
* 355. Design Twitter
13
-
*
14
-
* Design a simplified version of Twitter where users can post tweets,
15
-
* 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:
16
-
postTweet(userId, tweetId): Compose a new tweet.
17
-
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.
18
-
follow(followerId, followeeId): Follower follows a followee.
19
-
unfollow(followerId, followeeId): Follower unfollows a followee.
20
-
21
-
Example:
22
-
23
-
Twitter twitter = new Twitter();
24
-
25
-
// User 1 posts a new tweet (userId = 5).
26
-
twitter.postTweet(1, 5);
27
-
28
-
// User 1's news feed should return a list with 1 tweet userId -> [5].
29
-
twitter.getNewsFeed(1);
30
-
31
-
// User 1 follows user 2.
32
-
twitter.follow(1, 2);
33
-
34
-
// User 2 posts a new tweet (userId = 6).
35
-
twitter.postTweet(2, 6);
36
-
37
-
// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
38
-
// Tweet userId 6 should precede tweet userId 5 because it is posted after tweet userId 5.
39
-
twitter.getNewsFeed(1);
40
-
41
-
// User 1 unfollows user 2.
42
-
twitter.unfollow(1, 2);
43
-
44
-
// User 1's news feed should return a list with 1 tweet userId -> [5],
45
-
// since user 1 is no longer following user 2.
46
-
twitter.getNewsFeed(1);
47
-
*/
48
11
publicclass_355 {
49
12
50
13
publicstaticclassSolution1 {
@@ -60,9 +23,12 @@ class Tweet {
60
23
publicinttime;
61
24
publicintid;
62
25
publicTweetnext;
63
-
/**have a pointer,
26
+
27
+
/**
28
+
* have a pointer,
64
29
* so we could be more memory efficient when retrieving tweets,
65
-
* think about merging k sorted lists*/
30
+
* think about merging k sorted lists
31
+
*/
66
32
67
33
publicTweet(intid) {
68
34
this.id = id;
@@ -237,13 +203,17 @@ public Tweet(int id) {
237
203
}
238
204
}
239
205
240
-
/** Initialize your data structure here. */
206
+
/**
207
+
* Initialize your data structure here.
208
+
*/
241
209
publicTwitter() {
242
210
map = newHashMap<>();
243
211
timestamp = 0;
244
212
}
245
213
246
-
/** Compose a new tweet. */
214
+
/**
215
+
* Compose a new tweet.
216
+
*/
247
217
publicvoidpostTweet(intuserId, inttweetId) {
248
218
if (!map.containsKey(userId)) {
249
219
Useruser = newUser(userId);
@@ -252,7 +222,9 @@ public void postTweet(int userId, int tweetId) {
252
222
map.get(userId).postTweet(tweetId);
253
223
}
254
224
255
-
/** 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. */
225
+
/**
226
+
* 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.
227
+
*/
256
228
publicList<Integer> getNewsFeed(intuserId) {
257
229
List<Integer> result = newLinkedList<>();
258
230
if (!map.containsKey(userId)) {
@@ -282,7 +254,9 @@ public List<Integer> getNewsFeed(int userId) {
282
254
returnresult;
283
255
}
284
256
285
-
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
257
+
/**
258
+
* Follower follows a followee. If the operation is invalid, it should be a no-op.
259
+
*/
286
260
publicvoidfollow(intfollowerId, intfolloweeId) {
287
261
if (!map.containsKey(followerId)) {
288
262
map.put(followerId, newUser(followerId));
@@ -293,7 +267,9 @@ public void follow(int followerId, int followeeId) {
293
267
map.get(followerId).follow(followeeId);
294
268
}
295
269
296
-
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
270
+
/**
271
+
* Follower unfollows a followee. If the operation is invalid, it should be a no-op.
0 commit comments