首页 > 代码库 > Leetcode: Design Twitter

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

postTweet(userId, tweetId): Compose a new tweet.
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.
follow(followerId, followeeId): Follower follows a followee.
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);

注意需要maintain一个time stamp, 每次postTweet时++。还有要注意unfollow的时候,不能unfollow自己

  1 public class Twitter {
  2     class User {
  3         int userId;
  4         List<Tweet> tweets;
  5         Set<Integer> followees;
  6         public User(int id) {
  7             this.userId = id;
  8             this.tweets = new ArrayList<Tweet>();
  9             this.followees = new HashSet<Integer>();
 10             followees.add(id);
 11         }
 12     }
 13     
 14     class Tweet {
 15         int tweetId;
 16         int seq;
 17         public Tweet(int id, int num) {
 18             this.tweetId = id;
 19             this.seq = num;
 20         }
 21     }
 22     
 23     Map<Integer, User> userMap;
 24     PriorityQueue<Tweet> maxHeap;
 25     int seq;
 26 
 27     /** Initialize your data structure here. */
 28     public Twitter() {
 29         userMap = new HashMap<Integer, User>();
 30         maxHeap = new PriorityQueue<Tweet>(11, new Comparator<Tweet>() {
 31             public int compare(Tweet t1, Tweet t2) {
 32                 return t2.seq-t1.seq;
 33             }
 34         });
 35         this.seq = 0;
 36     }
 37     
 38     /** Compose a new tweet. */
 39     public void postTweet(int userId, int tweetId) {
 40         if (!userMap.containsKey(userId)) {
 41             User newUser = new User(userId);
 42             userMap.put(userId, newUser);
 43         }
 44         User curUser = userMap.get(userId);
 45         curUser.tweets.add(new Tweet(tweetId, ++this.seq));
 46     }
 47     
 48     /** 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. */
 49     public List<Integer> getNewsFeed(int userId) {
 50         List<Integer> res = new ArrayList<Integer>();
 51         if (!userMap.containsKey(userId)) return res;
 52         User curUser = userMap.get(userId);
 53         maxHeap.clear();
 54         for (int followeeId : curUser.followees) {
 55             List<Tweet> followeeTweets = userMap.get(followeeId).tweets;
 56             for (Tweet tweet : followeeTweets) {
 57                 maxHeap.offer(tweet);
 58             }
 59         }
 60         for (int i=0; i<10 && !maxHeap.isEmpty(); i++) {
 61             res.add(maxHeap.poll().tweetId);
 62         }
 63         return res;
 64     }
 65     
 66     /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
 67     public void follow(int followerId, int followeeId) {
 68         if (!userMap.containsKey(followerId)) {
 69             User newUser = new User(followerId);
 70             userMap.put(followerId, newUser);
 71         }
 72         if (!userMap.containsKey(followeeId)) {
 73             User newUser = new User(followeeId);
 74             userMap.put(followeeId, newUser);
 75         }
 76         userMap.get(followerId).followees.add(followeeId);
 77     }
 78     
 79     /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
 80     public void unfollow(int followerId, int followeeId) {
 81         if (!userMap.containsKey(followerId)) {
 82             User newUser = new User(followerId);
 83             userMap.put(followerId, newUser);
 84         }
 85         if (!userMap.containsKey(followeeId)) {
 86             User newUser = new User(followeeId);
 87             userMap.put(followeeId, newUser);
 88         }
 89         if (followerId != followeeId)
 90             userMap.get(followerId).followees.remove(followeeId);        
 91     }
 92 }
 93 
 94 /**
 95  * Your Twitter object will be instantiated and called as such:
 96  * Twitter obj = new Twitter();
 97  * obj.postTweet(userId,tweetId);
 98  * List<Integer> param_2 = obj.getNewsFeed(userId);
 99  * obj.follow(followerId,followeeId);
100  * obj.unfollow(followerId,followeeId);
101  */

 

关于PriorityQueue还看到这一种写法,挺简单的:

 1 Set<Integer> users = userMap.get(userId).followed;

2  PriorityQueue<Tweet> q = new PriorityQueue<Tweet>(users.size(), (a,b)->(b.time-a.time)); 

Leetcode: Design Twitter