forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign Twitter.java
More file actions
165 lines (133 loc) · 4.94 KB
/
Design Twitter.java
File metadata and controls
165 lines (133 loc) · 4.94 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class Twitter {
/** Initialize your data structure here. */
Map<Integer, User> userMap;
Map<User, List<User>> followingMap;
Map<User, List<User>> followerMap;
int tweetTimeStamp;
public Twitter() {
followingMap = new HashMap<>();
userMap = new HashMap<>();
followerMap = new HashMap<>();
tweetTimeStamp = 0;
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
Tweet tweet = new Tweet(tweetId, userId, tweetTimeStamp);
tweetTimeStamp++;
createUserIfAbsent(userId);
userMap.get(userId).topTweets.add(tweet);
List<User> followers = followerMap.getOrDefault(userMap.get(userId), new ArrayList<>());
for (User follower : followers) {
follower.topTweets.add(tweet);
}
}
/** 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) {
if (!userMap.containsKey(userId)) {
return new ArrayList<>();
}
List<Integer> topTweetIds = new ArrayList<>();
Set<Integer> tweetIdSet = new HashSet<>();
List<Tweet> topTweets = new ArrayList<>();
PriorityQueue<Tweet> pq = userMap.get(userId).topTweets;
int count = 10;
while (count > 0 && !pq.isEmpty()) {
Tweet tweet = pq.poll();
topTweets.add(tweet);
List<User> usersFollowing = followingMap.get(userMap.get(userId));
if ((userMap.containsKey(tweet.userId) && usersFollowing.contains(userMap.get(tweet.userId))) ||
tweet.userId == userId) {
if (!tweetIdSet.contains(tweet.tweetId)) {
topTweetIds.add(tweet.tweetId);
tweetIdSet.add(tweet.tweetId);
count--;
}
}
}
pq.addAll(topTweets);
return topTweetIds;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if (followeeId == followerId) {
return;
}
createUserIfAbsent(followeeId);
createUserIfAbsent(followerId);
if (!followingMap.get(userMap.get(followerId)).contains(userMap.get(followeeId))) {
followingMap.get(userMap.get(followerId)).add(userMap.get(followeeId));
userMap.get(followerId).topTweets.addAll(userMap.get(followeeId).topTweets);
}
if (!followerMap.getOrDefault(userMap.get(followeeId), new ArrayList<>()).contains(userMap.get(followerId))) {
followerMap.get(userMap.get(followeeId)).add(userMap.get(followerId));
}
}
private void createUserIfAbsent(int id) {
if (!userMap.containsKey(id)) {
User newUser = new User(id);
userMap.put(id, newUser);
List<User> followers = new ArrayList<>();
List<User> following = new ArrayList<>();
followingMap.put(newUser, following);
followerMap.put(newUser, followers);
}
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if (!userMap.containsKey(followeeId)) {
return;
}
if (!userMap.containsKey(followerId)) {
return;
}
if (followingMap.get(userMap.get(followerId)).contains(userMap.get(followeeId))) {
followingMap.get(userMap.get(followerId)).remove(userMap.get(followeeId));
}
}
}
class User {
public int id;
public List<Integer> followers;
PriorityQueue<Tweet> topTweets;
public User(int id) {
this.id = id;
this.followers = new ArrayList<>();
this.topTweets = new PriorityQueue<>(new Comparator<Tweet>() {
@Override
public int compare(Tweet o1, Tweet o2) {
return o2.timestamp - o1.timestamp;
}
});
}
@Override
public String toString() {
return "User{" +
"id=" + id +
'}';
}
}
class Tweet {
public int tweetId;
public int userId;
public int timestamp;
public Tweet(int tweetId, int userId, int timestamp) {
this.tweetId = tweetId;
this.userId = userId;
this.timestamp = timestamp;
}
@Override
public String toString() {
return "Tweet{" +
"tweetId=" + tweetId +
", userId=" + userId +
'}';
}
}
/**
* 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);
*/