Design Twitter LeetCode solution in JavaScript

1
var Twitter = function () {
2
this.users = new Map();
3
this.tweets = [];
4
};
5

6
Twitter.prototype.postTweet = function (userId, tweetId) {
7
if (!this.users.has(userId)) this.users.set(userId, new Set());
8
this.tweets.push({ userId, tweetId });
9
};
10

11
Twitter.prototype.getNewsFeed = function (userId) {
12
let user = this.users.get(userId);
13
let recentTweets = [];
14
for (
15
let i = this.tweets.length - 1;
16
i >= 0 && recentTweets.length < 10;
17
i--
18
) {
19
if (user.has(this.tweets[i].userId) || this.tweets[i].userId === userId) {
20
recentTweets.push(this.tweets[i].tweetId);
21
}
22
}
23
return recentTweets;
24
};
25

26
Twitter.prototype.follow = function (followerId, followeeId) {
27
let user = this.users.get(followerId) || new Set();
28
user.add(followeeId);
29
this.users.set(followerId, user);
30
};
31

32
Twitter.prototype.unfollow = function (followerId, followeeId) {
33
this.users.get(followerId)?.delete(followeeId);
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0