-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
155 lines (135 loc) · 3.61 KB
/
Copy pathsolution.java
File metadata and controls
155 lines (135 loc) · 3.61 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
// 355. Design Twitter
// https://leetcode.com/problems/design-twitter/
// Medium | Java | Accepted 2025-12-18
// Runtime 32 ms | Memory 56.6 MB
class Twitter {
private static int timeStamp = 0;
private Map<Integer, User> map;
private class Tweet implements Comparable<Tweet>
{
private int id;
private int time;
Tweet next;
public Tweet(int id)
{
this.id = id;
this.time = timeStamp;
timeStamp++;
next = null;
}
public int getTime()
{
return time;
}
public int getId()
{
return id;
}
public int compareTo(Tweet other) {
return Integer.compare(this.getTime(), other.getTime());
}
}
private class User {
private int uid;
private Set<Integer> follows;
private Tweet head;
public User (int id)
{
uid = id;
follows = new HashSet<>();
follow(id);
head = null;
}
public void postTweet(int tweetId)
{
Tweet tweet = new Tweet(tweetId);
tweet.next = head;
head = tweet;
}
public void follow(int followId)
{
follows.add(followId);
}
public void unfollow(int unfollowId)
{
follows.remove(unfollowId);
}
public Set<Integer> getFollows()
{
return follows;
}
public Tweet getHead()
{
return head;
}
}
public Twitter() {
map = new HashMap<>();
}
public void postTweet(int userId, int tweetId) {
if(map.containsKey(userId))
{
map.get(userId).postTweet(tweetId);
}
else
{
User user = new User(userId);
user.postTweet(tweetId);
map.put(userId, user);
}
}
public List<Integer> getNewsFeed(int userId) {
PriorityQueue<Tweet> queue = new PriorityQueue<>(Collections.reverseOrder());
if(!map.containsKey(userId))
{
map.put(userId, new User(userId));
}
Set<Integer> temp = map.get(userId).getFollows();
List<Integer> newsfeed = new ArrayList<>();
for(int id : temp)
{
if(map.get(id).getHead()!=null)
{
queue.add(map.get(id).getHead());
}
}
for(int i = 0; i<10; i++)
{
Tweet pop = queue.poll();
if(pop!=null)
{
newsfeed.add(pop.getId());
}
if(pop!=null && pop.next!=null)
{
queue.add(pop.next);
}
}
return newsfeed;
}
public void follow(int followerId, int followeeId) {
if(!map.containsKey(followerId))
{
map.put(followerId,new User(followerId));
}
if(!map.containsKey(followeeId))
{
map.put(followeeId,new User(followeeId));
}
map.get(followerId).follow(followeeId);
}
public void unfollow(int followerId, int followeeId) {
if(map.containsKey(followerId) && followerId != followeeId)
{
map.get(followerId).unfollow(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);
*/