forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path355_Twitter.java
116 lines (102 loc) · 3.78 KB
/
355_Twitter.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
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
public class Twitter {
private static int time = 0;
HashMap<Integer, List<Tweet>> tweets;
HashMap<Integer, Set<Integer>> relation;
public class Tweet {
int id;
int time;
public Tweet(int id, int time){
this.id = id;
this.time = time;
}
}
/**
* Initialize your data structure here.
*/
public Twitter() {
time = 0;
tweets = new HashMap<Integer, List<Tweet>>();
relation = new HashMap<Integer, Set<Integer>>();
}
/**
* Compose a new tweet.
*/
public void postTweet(int userId, int tweetId) {
List<Tweet> list = new LinkedList<Tweet>();
if (tweets.containsKey(userId)) {
list = tweets.get(userId);
}
list.add(0, new Tweet(tweetId, time++));
tweets.put(userId, list);
}
/**
* 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 = relation.get(userId);
List<Integer> newsFeed = mergeNewsFeed(userId, following);
return newsFeed;
}
private List<Integer> mergeNewsFeed(int userId, Set<Integer> following) {
Comparator<List<Tweet>> comparator = new Comparator<List<Tweet>>() {
public int compare(List<Tweet> a, List<Tweet> b) {
return b.get(0).time - a.get(0).time;
}
};
PriorityQueue<List<Tweet>> pq = new PriorityQueue<List<Tweet>>(comparator);
List<Tweet> currentNewsFeed = tweets.get(userId);
if (currentNewsFeed!= null && currentNewsFeed.size()!=0) {
List<Tweet> currentNewsFeedCopy = new LinkedList<Tweet>(currentNewsFeed);
pq.offer(currentNewsFeedCopy);
}
if (following != null) {
for (Integer followeeId : following) {
if(followeeId == userId) continue;
List<Tweet> followeeNewsFeed = tweets.get(followeeId);
if (followeeNewsFeed!= null && followeeNewsFeed.size()!=0) {
List<Tweet> followeeNewsFeedCopy = new LinkedList<Tweet>(followeeNewsFeed);
pq.offer(followeeNewsFeedCopy);
}
}
}
List<Integer> res = new ArrayList<Integer>();
int count = 10;
while (!pq.isEmpty() && count > 0) {
List<Tweet> temp = pq.poll();
int tweetId = temp.remove(0).id;
res.add(tweetId);
if (temp.size() != 0) pq.offer(temp);
count--;
}
return res;
}
/**
* Follower follows a followee. If the operation is invalid, it should be a no-op.
*/
public void follow(int followerId, int followeeId) {
Set<Integer> following = new HashSet<Integer>();
if (relation.containsKey(followerId)) {
following = relation.get(followerId);
}
following.add(followeeId);
relation.put(followerId, following);
}
/**
* Follower unfollows a followee. If the operation is invalid, it should be a no-op.
*/
public void unfollow(int followerId, int followeeId) {
if (relation.containsKey(followerId)) {
Set<Integer> following = relation.get(followerId);
following.remove(followeeId);
relation.put(followerId, following);
} else return;
}
}
/**
* 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);
*/