[B] [C] [D]

Design a simplified version of Twitter that allows users to send tweets, follow/unfollow other users, and see the last 10 tweets of those who follow them (including themselves).

Implement the Twitter class:

  • Twitter()Initialize a simple Twitter object
  • void postTweet(int userId, int tweetId)According to the giventweetId 和 userIdCreate a new tweet. A different one is used each time this function is calledtweetId 。
  • List<Integer> getNewsFeed(int userId)Retrieves the latest in the current user’s news feed10The ID of a tweet. Every item in the news feed must be a tweet from someone the user follows or from the user himself. Tweets mustSort in chronological order from most recent to most distant 。
  • void follow(int followerId, int followeeId)ID forfollowerIdUsers with ID asfolloweeIdTo the user.
  • void unfollow(int followerId, int followeeId)ID forfollowerIdUsers whose ID isfolloweeIdTo the user.

Example:

Enter ["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] Twitter = new Twitter(); twitter.postTweet(1, 5); // User 1 sends a new tweet (user ID = 1, Tweet ID = 5) twitter.getNewsfeed (1); Follow (1, 2); // User 1 should return a list of tweets with an id of 5. // User 1 follows user 2 twitter.posttweet (2, 6); // User 2 sends a new tweet (tweet id = 6) twitter.getNewsfeed (1); // User 1's fetch tweets should return a list of two tweets with ids -> [6, 5]. Tweet ID 6 should precede Tweet ID 5 because it was sent after 5. Unfollow (1, 2); // User 1 unfollows user 2 twitter.getNewsfeed (1); // When user 1 gets tweets, it should return a list of tweets with id 5. Because user 1 is no longer paying attention to user 2Copy the code

Tip:

  • 1 <= userId, followerId, followeeId <= 500
  • 0 <= tweetId <= 104
  • All tweets have different ids
  • postTweet,getNewsFeed,follow 和 unfollowMethod calls at most3 * 104 次

In this example, we need to maintain a list of users in the Twitter class

In addition, because tweets are sorted from near to far in time, you need an attribute that identifies the order of each tweet. Here, you can maintain an incremented ID in the Twitter class, assigning the class ID to the tweet ID every time a new tweet is sent, and then incrementing the class ID

Each user object should have two attributes: a List attribute that holds the user’s last 10 tweets (since you only need to get the last 10 tweets to get tweets, you don’t need to keep any earlier tweets) and a FOLLOWS attribute that holds a list of the users that the current user follows

To get a tweet, you need to get the last 10 tweets sent by the current user and the last 10 tweets from people who follow them, and sort these tweets in chronological order from nearest to far. The first 10 are the results required by the getNewsFeed method

The follow action is to add the follower ID to the follower FOLLOWS list

The unfollow operation removes the follower ID from the follower follows list

The code is as follows:

Var Twitter = function() {this.id = 0; // List of users this.users = {}; }; /** * @param {number} userId * @param {number} tweetId * @return {void} */ Twitter.prototype.postTweet = Function (userId, tweetId) {function(userId, tweetId) {// If (! this.users[userId]) this.users[userId] = { list:[], Follows :new Set()} this.users[userId].list.push({id: this.ID ++,tweetId} if(this.users[userId].list.length>10) this.users[userId].list.shift(); }; /** * @param {number} userId * @return {number[]} */ Twitter.prototype.getNewsFeed = function(userId) { // [] if(!) [] if(!) [] if(! this.users[userId]) return []; Let arr = this.users[userId].list; let arr = this.users[userId].list; this.users[userId].follows.forEach(item => { if(this.users[item]){ arr = [...arr,...this.users[item].list] } }) // Sort ((a,b) => b.id-a.id); // Get the id of the last ten tweets const res = []; for(let i = 0; i<Math.min(arr.length,10); i++){ res.push(arr[i].tweetId) } return res; }; /** * @param {number} followerId * @param {number} followeeId * @return {void} */ Twitter.prototype.follow = Function (followerId, followeeId) {// If (! this.users[followerId]) this.users[followerId] = { list:[], Follows :new Set()} this.users[followerId].follows. Add (followeeId)}; /** * @param {number} followerId * @param {number} followeeId * @return {void} */ Twitter.prototype.unfollow = Function (followerId, followeeId) {this.users[followerId].follows. Delete (followeeId)};Copy the code

At this point we are done with Leetcode-355-Design Twitter

If you have any questions or suggestions, please leave a comment!