-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitter.java
More file actions
96 lines (84 loc) · 3.43 KB
/
Twitter.java
File metadata and controls
96 lines (84 loc) · 3.43 KB
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
package leetCode_355;
import java.util.*;
/**
* @author dimdark
* @date 2017-08-27
* @time 6:04 PM
*/
public class Twitter {
// inner class
class Tweet implements Comparable<Tweet> {
Integer tweetId;
Long createTime;
public Tweet(Integer tweetId, Long createTime) {
this.tweetId = tweetId;
this.createTime = createTime;
}
public int compareTo(Tweet o) {
if(o == null) throw new NullPointerException("o is null");
if(this.createTime > o.createTime) return -1;
else if(this.createTime < o.createTime) return 1;
else return 0;
}
}
private Map<Integer, LinkedList<Tweet>> userToTweets;
private Map<Integer, LinkedList<Integer>> userToFollowees;
private static final int NEWS_CNT = 10;
private static long ALL_TWEET_CNT = 0;
/** Initialize your data structure here. */
public Twitter() {
userToTweets = new HashMap<Integer, LinkedList<Tweet>>();
userToFollowees = new HashMap<Integer, LinkedList<Integer>>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
Tweet tweet = new Tweet(tweetId, ++ALL_TWEET_CNT);
if(userToTweets.containsKey(userId)) {
userToTweets.get(userId).addLast(tweet);
}else { // first post tweet
follow(userId, userId); // follow myself
LinkedList<Tweet> tweets = new LinkedList<Tweet>();
tweets.add(tweet);
userToTweets.put(userId, tweets);
}
}
/** 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> recentTweets = new LinkedList<Integer>();
LinkedList<Integer> followees = userToFollowees.get(userId);
if(followees == null) return recentTweets;
Integer[] followeeIds = followees.toArray(new Integer[0]);
List<Tweet> tweets = new ArrayList<Tweet>();
for(int followeeId : followeeIds) {
if(userToTweets.containsKey(followeeId)) {
tweets.addAll(userToTweets.get(followeeId));
}
}
Collections.sort(tweets);
for(int i = 0; i < NEWS_CNT; ++i) {
if(i >= tweets.size()) break;
recentTweets.add(tweets.get(i).tweetId);
}
return recentTweets;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
LinkedList<Integer> followees = userToFollowees.get(followerId);
if(followees != null) {
if(!followees.contains(followeeId)){
followees.add(followeeId);
}
}else {
followees = new LinkedList<Integer>();
followees.add(followeeId);
userToFollowees.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(followerId == followeeId) return;
LinkedList<Integer> followees = userToFollowees.get(followerId);
if(followees == null || !followees.contains(followeeId)) return;
followees.remove(Integer.valueOf(followeeId));
}
}