-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
77 lines (69 loc) · 3.13 KB
/
Main.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
import java.util.*;
public class DesignTwitter355 {
class Twitter {
HashMap<Integer, HashSet<Integer>> followingMap;
HashMap<Integer, LinkedList<Integer>> tweetMap;
HashMap<Integer, Integer> tweetIds;
/** Initialize your data structure here. */
public Twitter() {
this.followingMap = new HashMap<>();
this.tweetMap = new HashMap<>();
this.tweetIds = new HashMap<>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
if (this.tweetIds.containsKey(tweetId)) {
return;
}
LinkedList<Integer> posts
= this.tweetMap.getOrDefault(userId, new LinkedList<Integer>());
posts.addFirst(tweetId);
this.tweetMap.put(userId, posts);
this.tweetIds.put(tweetId, this.tweetIds.size());
}
/** 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) {
HashSet<Integer> followings = this.followingMap.getOrDefault(userId, new HashSet<>());
PriorityQueue<LinkedList<Integer>> pq
= new PriorityQueue<>(
followings.size() + 1,
(x, y) -> this.tweetIds.get(y.getFirst()) - this.tweetIds.get(x.getFirst())
);
followings = new HashSet<>(followings);
followings.add(userId);
for (int followee : followings) {
LinkedList<Integer> list = this.tweetMap.getOrDefault(followee, new LinkedList<>());
if (list.size() == 0) {
continue;
}
pq.offer(new LinkedList<>(list));
}
LinkedList<Integer> news = new LinkedList<>();
for (int i = 0; i < 10 && !pq.isEmpty(); i++) {
LinkedList<Integer> recentest = pq.poll();
news.add(recentest.pollFirst());
if (recentest.size() != 0) {
pq.offer(recentest);
}
}
return news;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
HashSet<Integer> followings
= this.followingMap.getOrDefault(followerId, new HashSet<>());
followings.add(followeeId);
this.followingMap.put(followerId, followings);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
HashSet<Integer> followings
= this.followingMap.getOrDefault(followerId, new HashSet<>());
if (!followings.contains(followeeId)) {
return;
}
followings.remove(followeeId);
this.followingMap.put(followerId, followings);
}
}
}