forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
60 lines (54 loc) · 2.38 KB
/
Solution.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
class Twitter {
private Map<Integer, List<Integer>> userTweets;
private Map<Integer, Set<Integer>> userFollowing;
private Map<Integer, Integer> tweets;
private int time;
/** Initialize your data structure here. */
public Twitter() {
userTweets = new HashMap<>();
userFollowing = new HashMap<>();
tweets = new HashMap<>();
time = 0;
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
userTweets.computeIfAbsent(userId, k -> new ArrayList<>()).add(tweetId);
tweets.put(tweetId, ++time);
}
/** 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) {
Set<Integer> following = userFollowing.getOrDefault(userId, new HashSet<>());
Set<Integer> users = new HashSet<>(following);
users.add(userId);
PriorityQueue<Integer> pq = new PriorityQueue<>(10, (a, b) -> (tweets.get(b) - tweets.get(a)));
for (Integer u : users) {
List<Integer> userTweet = userTweets.get(u);
if (userTweet != null && !userTweet.isEmpty()) {
for (int i = userTweet.size() - 1, k = 10; i >= 0 && k > 0; --i, --k) {
pq.offer(userTweet.get(i));
}
}
}
List<Integer> res = new ArrayList<>();
while (!pq.isEmpty() && res.size() < 10) {
res.add(pq.poll());
}
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).remove(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);
*/