|
| 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