Skip to content

Commit 9d00d53

Browse files
committed
Add solution #355
1 parent 546d3c2 commit 9d00d53

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
349|[Intersection of Two Arrays](./0349-intersection-of-two-arrays.js)|Easy|
252252
350|[Intersection of Two Arrays II](./0350-intersection-of-two-arrays-ii.js)|Easy|
253253
352|[Data Stream as Disjoint Intervals](./0352-data-stream-as-disjoint-intervals.js)|Hard|
254+
355|[Design Twitter](./0355-design-twitter.js)|Medium|
254255
367|[Valid Perfect Square](./0367-valid-perfect-square.js)|Easy|
255256
371|[Sum of Two Integers](./0371-sum-of-two-integers.js)|Medium|
256257
372|[Super Pow](./0372-super-pow.js)|Medium|

solutions/0355-design-twitter.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* 355. Design Twitter
3+
* https://leetcode.com/problems/design-twitter/
4+
* Difficulty: Medium
5+
*
6+
* Design a simplified version of Twitter where users can post tweets, follow/unfollow another user,
7+
* and is able to see the 10 most recent tweets in the user's news feed.
8+
*
9+
* Implement the Twitter class:
10+
* - Twitter() Initializes your twitter object.
11+
* - void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user
12+
* userId. Each call to this function will be made with a unique tweetId.
13+
* - List<Integer> getNewsFeed(int userId) Retrieves the 10 most recent tweet IDs in the user's news
14+
* feed. Each item in the news feed must be posted by users who the user followed or by the user
15+
* themself. Tweets must be ordered from most recent to least recent.
16+
* - void follow(int followerId, int followeeId) The user with ID followerId started following the
17+
* user with ID followeeId.
18+
* - void unfollow(int followerId, int followeeId) The user with ID followerId started unfollowing
19+
* the user with ID followeeId.
20+
*/
21+
22+
23+
var Twitter = function() {
24+
this.tweets = [];
25+
this.followers = new Map();
26+
};
27+
28+
/**
29+
* @param {number} userId
30+
* @param {number} tweetId
31+
* @return {void}
32+
*/
33+
Twitter.prototype.postTweet = function(userId, tweetId) {
34+
this.tweets.unshift([userId, tweetId]);
35+
};
36+
37+
/**
38+
* @param {number} userId
39+
* @return {number[]}
40+
*/
41+
Twitter.prototype.getNewsFeed = function(userId) {
42+
const result = [];
43+
for (let i = 0; i < this.tweets.length && result.length < 10; i++) {
44+
const [user, tweet] = this.tweets[i] ?? [];
45+
if (user === userId || (this.followers.get(userId) && this.followers.get(userId).has(user))) {
46+
result.push(tweet);
47+
}
48+
}
49+
return result;
50+
};
51+
52+
/**
53+
* @param {number} followerId
54+
* @param {number} followeeId
55+
* @return {void}
56+
*/
57+
Twitter.prototype.follow = function(followerId, followeeId) {
58+
if (followerId !== followeeId) {
59+
if (!this.followers.has(followerId)) {
60+
this.followers.set(followerId, new Set());
61+
}
62+
this.followers.get(followerId).add(followeeId);
63+
}
64+
};
65+
66+
/**
67+
* @param {number} followerId
68+
* @param {number} followeeId
69+
* @return {void}
70+
*/
71+
Twitter.prototype.unfollow = function(followerId, followeeId) {
72+
if (this.followers.has(followerId)) {
73+
this.followers.get(followerId).delete(followeeId);
74+
}
75+
};

0 commit comments

Comments
 (0)