355. Design Twitter

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);
 
The good design about this implementation is that we keep the previous Tweet for the same user in the "next" field (at line 8), rather than keep all tweets in a queue inside the user. The benefit of doing this is that, when we get the top 10 feeds (at line 59), we only need to keep the head post of each follower into a pq, rather than all posts. We just compare the head, and get the next one after head (line 69). Similar problems are here.
 
  1 class Twitter {
  2   private static int timeStamp = 0;
  3   private Map<Integer, User> userMap = new HashMap<>();
  4 
  5   private class Tweet {
  6     int tweetId;
  7     int timeStamp;
  8     Tweet next;
  9 
 10     Tweet(int tweetId) {
 11       this.tweetId = tweetId;
 12       timeStamp = Twitter.timeStamp++;
 13     }
 14   }
 15 
 16   private class User {
 17     int userId;
 18     Set<Integer> myFollows = new HashSet<>(); //Show the other users whom current user is following
 19     Tweet myPosts; //Keep user's own posts in a timed order.
 20 
 21     User(int userId) {
 22       this.userId = userId;
 23       follow(userId); // first follow itself
 24     }
 25 
 26     void follow(int id) {
 27       myFollows.add(id);
 28     }
 29 
 30     void unfollow(int id) {
 31       myFollows.remove(id);
 32     }
 33 
 34     void post(int id) {
 35       Tweet t = new Tweet(id);
 36       t.next = myPosts;
 37       myPosts = t; // The head keeps the latest post.
 38     }
 39   }
 40 
 41   /**
 42    * Compose a new tweet.
 43    */
 44   public void postTweet(int userId, int tweetId) {
 45     getOrCreateUser(userId).post(tweetId);
 46   }
 47 
 48   /**
 49    * 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 myFollows or by the user herself. Tweets must be ordered from most recent to least recent.
 50    */
 51   public List<Integer> getNewsFeed(int userId) {
 52     List<Integer> feeds = new LinkedList<>();
 53     User user = userMap.get(userId);
 54     if (user == null)
 55       return feeds;
 56 
 57     PriorityQueue<Tweet> q = new PriorityQueue<>(user.myFollows.size(), (a, b) -> (b.timeStamp - a.timeStamp));
 58     for (int f : user.myFollows) {
 59       Tweet t = userMap.get(f).myPosts;
 60       if (t != null)
 61         q.add(t); //add all followed tweets to one PQ.
 62     }
 63 
 64     int n = 0;
 65     while (!q.isEmpty() && n++ < 10) {
 66       Tweet t = q.poll();
 67       feeds.add(t.tweetId);
 68       if (t.next != null)
 69         q.add(t.next);
 70     }
 71     return feeds;
 72   }
 73 
 74   /**
 75    * Follower myFollows a followee. If the operation is invalid, it should be a no-op.
 76    */
 77   public void follow(int followerId, int followeeId) {
 78     getOrCreateUser(followerId).follow(getOrCreateUser(followeeId).userId);
 79   }
 80 
 81   /**
 82    * Follower unfollows a followee. If the operation is invalid, it should be a no-op.
 83    */
 84   public void unfollow(int followerId, int followeeId) {
 85     if (!userMap.containsKey(followerId) || followerId == followeeId)
 86       return;
 87     userMap.get(followerId).unfollow(followeeId);
 88   }
 89 
 90   private User getOrCreateUser(int userId) {
 91     User user = userMap.get(userId);
 92     if (user == null) {
 93       user = new User(userId);
 94       userMap.put(userId, user);
 95     }
 96     return user;
 97   }
 98 }
 99 
100 /**
101  * Your Twitter object will be instantiated and called as such:
102  * Twitter obj = new Twitter();
103  * obj.postTweet(userId,tweetId);
104  * List<Integer> param_2 = obj.getNewsFeed(userId);
105  * obj.follow(followerId,followeeId);
106  * obj.unfollow(followerId,followeeId);
107  */

 

 
 
 
 
posted @ 2016-07-25 08:04  新一代的天皇巨星  阅读(196)  评论(0)    收藏  举报