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.

示例1:

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

解法

解法一:

使用一个哈希表保存用户follow的用户;使用一个哈希表保存用户自己发送的tweet

JAVA

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
class Twitter {
private int tweetIndex = 1;

private Map<Integer, Set<Integer>> userFollowers;

private Map<Integer, PriorityQueue<Tweet>> userTweets;

/** Initialize your data structure here. */
public Twitter() {
userFollowers = new HashMap<>();
userTweets = new HashMap<>();
}

/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
Tweet tweet = new Tweet(userId, tweetId, tweetIndex++);
if (userTweets.containsKey(userId)) {
PriorityQueue<Tweet> queue = userTweets.get(userId);
queue.add(tweet);
if (queue.size() > 10) {
queue.poll();
}
userTweets.put(userId, queue);
} else {
PriorityQueue<Tweet> queue = new PriorityQueue<>((t1, t2) -> t1.getCurrentTime() - t2.getCurrentTime());
queue.add(tweet);
userTweets.put(userId, queue);
}
}

/** 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) {
PriorityQueue<Tweet> queue = new PriorityQueue<>((t1, t2) -> t2.getCurrentTime() - t1.getCurrentTime());
if (userTweets.containsKey(userId)) {
queue.addAll(userTweets.get(userId));
}

if (userFollowers.containsKey(userId)) {
Set<Integer> followers = userFollowers.get(userId);

for (int follower : followers) {
if (userTweets.containsKey(follower)) {
queue.addAll(userTweets.get(follower));
}
}
}

List<Integer> result = new ArrayList<Integer>(10);
if (!queue.isEmpty()) {
int index = 0;
while (index < 10 && !queue.isEmpty() ) {
result.add(queue.poll().getTweetId());
index++;
}
return result;
}
return result;
}

/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if (followerId == followeeId) {
return;
}

if (userFollowers.containsKey(followerId)) {
userFollowers.get(followerId).add(followeeId);
} else {
Set<Integer> followees = new HashSet<>();
followees.add(followeeId);
userFollowers.put(followerId, followees);
}
}

/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if (userFollowers.containsKey(followerId)) {
userFollowers.get(followerId).remove(followeeId);
}
}
}
0%