-
Notifications
You must be signed in to change notification settings - Fork 43
/
DesignTwitter.java
189 lines (157 loc) · 6.11 KB
/
DesignTwitter.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package LeetCodeJava.Design;
// https://leetcode.com/problems/design-twitter/
import javafx.util.Pair;
import java.util.*;
public class DesignTwitter {
// V0
// TODO : implement
// V1
// https://leetcode.com/problems/design-twitter/solutions/2720611/java-simple-hashmap-stack/
class Twitter_1 {
Stack<Pair<Integer,Integer>> tweets;
HashMap<Integer, Set<Integer>> network;
public Twitter_1() {
tweets = new Stack<>();
network = new HashMap<>();
}
public void postTweet(int userId, int tweetId) {
tweets.push(new Pair(userId, tweetId));
}
public List<Integer> getNewsFeed(int userId) {
Stack<Pair<Integer, Integer>> store = new Stack<>();
List<Integer> ans = new ArrayList<>();
Set<Integer> following = network.getOrDefault(userId, new HashSet<>());
while(tweets.size()!=0 && ans.size()!=10){
Pair<Integer, Integer> curr = tweets.pop();
if((following.contains(curr.getKey()) || curr.getKey()==userId)
&& ans.size()!=10) ans.add(curr.getValue());
store.push(curr);
}
while(store.size()!=0) tweets.push(store.pop());
return ans;
}
public void follow(int followerId, int followeeId) {
Set<Integer> following = network.getOrDefault(followerId, new HashSet<>());
following.add(followeeId);
network.put(followerId, following);
}
public void unfollow(int followerId, int followeeId) {
Set<Integer> following = network.get(followerId);
if(following == null) return;
following.remove(followeeId);
network.put(followerId, following);
}
}
// V1_1
// https://leetcode.com/problems/design-twitter/solutions/3869194/beats-100-8ms-java/
class Twitter_1_1 {
ArrayList<Tweet> tweets;
HashMap<Integer, User> users;
public Twitter_1_1() {
tweets = new ArrayList<>();
users = new HashMap<>();
}
public void postTweet(int userId, int tweetId) {
if(!users.containsKey(userId))
users.put(userId, new User(userId));
tweets.add(new Tweet(tweetId, userId));
}
public List<Integer> getNewsFeed(int userId) {
if(!users.containsKey(userId))
users.put(userId, new User(userId));
List<Integer> ans = new ArrayList<Integer>();
int i = tweets.size()-1;
while(ans.size()<10 && i>=0) {
if(tweets.get(i).userId==userId || users.get(userId).follows.contains(tweets.get(i).userId))
ans.add(tweets.get(i).tweetId);
i--;
}
return ans;
}
public void follow(int followerId, int followeeId) {
if(!users.containsKey(followerId))
users.put(followerId, new User(followerId));
if(!users.containsKey(followeeId))
users.put(followeeId, new User(followeeId));
User user = users.get(followerId);
user.follows.add(followeeId);
}
public void unfollow(int followerId, int followeeId) {
if(!users.containsKey(followerId))
users.put(followerId, new User(followerId));
if(!users.containsKey(followeeId))
users.put(followeeId, new User(followeeId));
User user = users.get(followerId);
user.follows.remove(followeeId);
}
}
class Tweet {
int tweetId;
int userId;
Tweet(int t, int u) {
tweetId = t;
userId = u;
}
}
class User {
int userId;
HashSet<Integer> follows;
User(int u) {
userId = u;
follows = new HashSet<>();
}
}
// V2
// https://leetcode.com/problems/design-twitter/solutions/3400455/simple-solution-for-beginners-with-step-by-step-explanation/
class Twitter_3 {
HashMap<Integer,ArrayList<Pair<Integer,Integer>>> posttweet ; // map<userid, < tweetId, time>>
HashMap<Integer,Set<Integer>> followers ;
int time;
public Twitter_3() {
posttweet = new HashMap<>();
followers = new HashMap<>();
time=0;
}
public void postTweet(int userId, int tweetId) {
Pair<Integer,Integer> pair = new Pair<>(tweetId,time);
ArrayList temp = posttweet.getOrDefault(userId,new ArrayList<>());
temp.add(pair);
posttweet.put(userId,temp);
time++;
}
public List<Integer> getNewsFeed(int userId) {
List<Integer> list = new ArrayList<>();
Set<Integer> set = followers.getOrDefault(userId,new HashSet<>());
set.add(userId);
list.addAll(set);
HashMap<Integer,Integer> map = new HashMap<>();
for(int i : list){
if(posttweet.get(i)!=null)
for(Pair<Integer,Integer> j : posttweet.get(i)){
map.put(j.getKey(),j.getValue());
}
}
PriorityQueue<Map.Entry<Integer,Integer>> pq = new PriorityQueue<>(((x,y)->x.getValue()-y.getValue()));
pq.addAll(map.entrySet());
while(pq.size()>10){
pq.poll();
}
List<Integer> ans = new ArrayList<>();
while(pq.size()!=0){
ans.add(pq.poll().getKey());
}Collections.reverse(ans);
return ans;
}
public void follow(int followerId, int followeeId) {
Set<Integer> set = followers.getOrDefault(followerId, new HashSet<>());
set.add(followeeId);
followers.put(followerId,set);
}
public void unfollow(int followerId, int followeeId) {
Set<Integer> set = followers.getOrDefault(followerId, new HashSet<>());
if(set.contains(followeeId))
set.remove(followeeId);
followers.put(followerId,set);
}
}
}