355. Design Twitter

Posted andywu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了355. Design Twitter相关的知识,希望对你有一定的参考价值。

Design a simplified version of Twitter where users can post tweets, 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:

 

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. unfollow(followerId, followeeId): Follower unfollows a followee.

 

Example:

Twitter twitter = new Twitter();

// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);

// User 1‘s news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);

// User 1 follows user 2.
twitter.follow(1, 2);

// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);

// User 1‘s news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);

// User 1 unfollows user 2.
twitter.unfollow(1, 2);

// User 1‘s news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);
题目含义:设计一个tweeter系统,用户可以发新闻,follow/unfollow可以查看到该用户的最新10条新闻。设计的系统主要包括以下几个函数

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. unfollow(followerId, followeeId): Follower unfollows a followee.
  1 class Twitter {
  2     
  3 
  4     // Tweet link to next Tweet so that we can save a lot of time
  5     // when we execute getNewsFeed(userId)
  6     class Tweet{
  7         public int id;
  8         public int time;
  9         public Tweet next;
 10         public Tweet(int id){
 11             this.id = id;
 12             time = timeStamp++;
 13             next=null;
 14         }
 15     }
 16     
 17  private static int timeStamp=0;
 18 
 19     // easy to find if user exist
 20     private Map<Integer, User> userMap;
 21 
 22 
 23 
 24     // OO design so User can follow, unfollow and post itself
 25     public class User{
 26         public int id;
 27         public Set<Integer> followed;
 28         public Tweet tweet_head;
 29 
 30         public User(int id){
 31             this.id=id;
 32             followed = new HashSet<>();
 33             follow(id); // first follow itself
 34             tweet_head = null;
 35         }
 36 
 37         public void follow(int id){
 38             followed.add(id);
 39         }
 40 
 41         public void unfollow(int id){
 42             followed.remove(id);
 43         }
 44 
 45 
 46         // everytime user post a new tweet, add it to the head of tweet list.
 47         public void post(int id){
 48             Tweet t = new Tweet(id);
 49             t.next=tweet_head;
 50             tweet_head=t;
 51         }
 52     }
 53 
 54 
 55 
 56 
 57     /** Initialize your data structure here. */
 58     public Twitter() {
 59         userMap = new HashMap<Integer, User>();
 60     }
 61 
 62     /** Compose a new tweet. */
 63     public void postTweet(int userId, int tweetId) {
 64         if(!userMap.containsKey(userId)){
 65             User u = new User(userId);
 66             userMap.put(userId, u);
 67         }
 68         userMap.get(userId).post(tweetId);
 69 
 70     }
 71 
 72 
 73 
 74     // Best part of this.
 75     // first get all tweets lists from one user including itself and all people it followed.
 76     // Second add all heads into a max heap. Every time we poll a tweet with
 77     // largest time stamp from the heap, then we add its next tweet into the heap.
 78     // So after adding all heads we only need to add 9 tweets at most into this
 79     // heap before we get the 10 most recent tweet.
 80     public List<Integer> getNewsFeed(int userId) {
 81         List<Integer> res = new LinkedList<>();
 82 
 83         if(!userMap.containsKey(userId))   return res;
 84 
 85         Set<Integer> users = userMap.get(userId).followed;
 86         PriorityQueue<Tweet> q = new PriorityQueue<Tweet>(users.size(), (a,b)->(b.time-a.time));
 87         for(int user: users){
 88             Tweet t = userMap.get(user).tweet_head;
 89             // very imporant! If we add null to the head we are screwed.
 90             if(t!=null){
 91                 q.add(t);
 92             }
 93         }
 94         int n=0;
 95         while(!q.isEmpty() && n<10){
 96             Tweet t = q.poll();
 97             res.add(t.id);
 98             n++;
 99             if(t.next!=null)
100                 q.add(t.next);
101         }
102 
103         return res;
104 
105     }
106 
107     /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
108     public void follow(int followerId, int followeeId) {
109         if(!userMap.containsKey(followerId)){
110             User u = new User(followerId);
111             userMap.put(followerId, u);
112         }
113         if(!userMap.containsKey(followeeId)){
114             User u = new User(followeeId);
115             userMap.put(followeeId, u);
116         }
117         userMap.get(followerId).follow(followeeId);
118     }
119 
120     /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
121     public void unfollow(int followerId, int followeeId) {
122         if(!userMap.containsKey(followerId) || followerId==followeeId)
123             return;
124         userMap.get(followerId).unfollow(followeeId);
125     }
126 }
127 
128 /**
129  * Your Twitter object will be instantiated and called as such:
130  * Twitter obj = new Twitter();
131  * obj.postTweet(userId,tweetId);
132  * List<Integer> param_2 = obj.getNewsFeed(userId);
133  * obj.follow(followerId,followeeId);
134  * obj.unfollow(followerId,followeeId);
135  */

 




以上是关于355. Design Twitter的主要内容,如果未能解决你的问题,请参考以下文章

355. Design Twitter

355. Design Twitter

355. Design Twitter

355. Design Twitter - Medium

leetcode@ [355] Design Twitter (Object Oriented Programming)

[LeetCode] 355. Design Twitter 设计推特