码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-Design Twitter

时间:2016-07-24 12:06:28      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

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

Analysis:
1. getNewsFeed() should be quick. We can store a user‘s tweets in a list, then in the call of this function, we find out all followed users and essentially do a merge on the most recent 10 tweets of these users (including @userId himself).

To make this quick, we can make Tweet class as a link list. So a PriorityQueue<Tweet> should be enough to achieve all the work.


Solution:
 1 public class Twitter {
 2     private static int timestamp = 0;
 3     
 4     public class Tweet {
 5         int tweetId;
 6         int userId;
 7         int time;
 8         Tweet next;
 9         
10         public Tweet(int uid, int tid){
11             tweetId = tid;
12             userId = uid;
13             time = timestamp++;
14             next = null;
15         }
16        
17     }
18 
19     /** Initialize your data structure here. */
20     public Twitter() {
21         tweetMap = new HashMap<Integer,Tweet>();
22         followMap = new HashMap<Integer,HashSet<Integer>>();
23         
24     }
25     
26     /** Compose a new tweet. */
27     public void postTweet(int userId, int tweetId) {
28         Tweet t = new Tweet(userId,tweetId);
29         t.next = tweetMap.get(userId);
30         tweetMap.put(userId,t);
31     }
32     
33     /** 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. */
34     public List<Integer> getNewsFeed(int userId) {
35         List<Integer> resList = new LinkedList<Integer>();
36         PriorityQueue<Tweet> q = new PriorityQueue<Tweet>((a,b) -> (b.time - a.time));
37         
38         follow(userId,userId);
39         HashSet<Integer> followSet = followMap.get(userId);
40         if (followSet != null){
41             for (Integer id : followSet)
42                 if (tweetMap.containsKey(id)) q.add(tweetMap.get(id));
43         }
44         
45         while (resList.size()<10 && !q.isEmpty()){
46             Tweet t = q.poll();
47             resList.add(t.tweetId);
48             if (t.next!=null) q.add(t.next);
49         }
50         
51         
52         return resList;
53     }
54     
55     /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
56     public void follow(int followerId, int followeeId) {
57         if (followMap.containsKey(followerId)){
58             followMap.get(followerId).add(followeeId);
59         } else {
60             HashSet<Integer> set = new HashSet<Integer>();
61             set.add(followeeId);
62             followMap.put(followerId,set);
63         }
64     }
65     
66     /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
67     public void unfollow(int followerId, int followeeId) {
68         if (followMap.containsKey(followerId)){
69             followMap.get(followerId).remove(followeeId);
70         }
71     }
72     
73     HashMap<Integer,Tweet> tweetMap;
74     HashMap<Integer,HashSet<Integer>> followMap;
75 }
76 
77 /**
78  * Your Twitter object will be instantiated and called as such:
79  * Twitter obj = new Twitter();
80  * obj.postTweet(userId,tweetId);
81  * List<Integer> param_2 = obj.getNewsFeed(userId);
82  * obj.follow(followerId,followeeId);
83  * obj.unfollow(followerId,followeeId);
84  */

 

LeetCode-Design Twitter

标签:

原文地址:http://www.cnblogs.com/lishiblog/p/5700358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!