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:

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);

解法1: Heap

用OOD设计的方法解这题比较清楚明白。在设计getNewsFeed的时候,用一个priorityQueue来把每一个follow的user的tweet的head放入堆中,按照时间顺序出堆入堆。每出一个就把当前指向的tweet的下一个推入堆中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class Twitter {
/** Initialize your data structure here. */
class Tweet {
int id;
int time;
Tweet next;
public Tweet(int id) {
this.id = id;
this.time = timeStamp++;
next = null;
}
};
class User {
int id;
Set<Integer> followed;
Tweet tweets;
public User(int id) {
this.id = id;
followed = new HashSet<>();
tweets = null;
follow(id);
}
public void post(int tweetId) {
Tweet tweet = new Tweet(tweetId);
tweet.next = tweets;
tweets = tweet;
}
public void follow(int id) {
if (!followed.contains(id)) {
followed.add(id);
}
}
public void unfollow(int id) {
if (followed.contains(id)) {
followed.remove(id);
}
}
};
int timeStamp = 0;
Map<Integer, User> userTable;
public Twitter() {
userTable = new HashMap<>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
if (!userTable.containsKey(userId)) {
userTable.put(userId, new User(userId));
}
User user = userTable.get(userId);
user.post(tweetId);
}
/** 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. */
public List<Integer> getNewsFeed(int userId) {
List<Integer> res = new ArrayList<>();
if (!userTable.containsKey(userId)) {
return res;
}
User user = userTable.get(userId);
Set<Integer> followed = user.followed;
PriorityQueue<Tweet> queue = new PriorityQueue<>((a,b) -> b.time - a.time);
for (Integer temp : followed) {
User followed_user = userTable.get(temp);
if (followed_user.tweets != null) {
queue.offer(followed_user.tweets);
}
}
int n = 0;
while (!queue.isEmpty() && n < 10) {
Tweet tweet = queue.poll();
res.add(tweet.id);
n++;
if (tweet.next != null) {
queue.offer(tweet.next);
}
}
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if (!userTable.containsKey(followerId)) {
userTable.put(followerId, new User(followerId));
}
if (!userTable.containsKey(followeeId)) {
userTable.put(followeeId, new User(followeeId));
}
userTable.get(followerId).follow(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if (!userTable.containsKey(followerId) || followerId == followeeId) {
return;
}
userTable.get(followerId).unfollow(followeeId);
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/