forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitter.java
204 lines (181 loc) · 5.91 KB
/
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package design;
import java.util.*;
/**
* Created by gouthamvidyapradhan on 18/03/2017.
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:
postTweet(userId, tweetId): Compose a new tweet.
getNewsFeed(userId): 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.
follow(followerId, followeeId): Follower follows a followee.
unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
Twitter twitter = new Twitter();
// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);
// User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);
// User 1 follows user 2.
twitter.follow(1, 2);
// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);
// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);
// User 1 unfollows user 2.
twitter.unfollow(1, 2);
// User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);
*/
public class Twitter
{
class User
{
int id;
Set<Integer> follow = new HashSet<>();
Queue<Tweet> tweets = new ArrayDeque<>();
User(int id)
{
this.id = id;
}
public void follow(int id)
{
follow.add(id);
}
public void addTweet(Tweet t)
{
if(tweets.size() == 10)
{
tweets.poll();
}
tweets.offer(t);
}
public void unFollow(int id)
{
follow.remove(id);
}
public Set<Integer> getFollow() {
return follow;
}
public Queue<Tweet> getTweets() {
return tweets;
}
}
class Tweet
{
int id;
long time;
Tweet(int id, long time)
{
this.id = id;
this.time = time;
}
}
private Map<Integer, User> userMap;
private Map<Integer, Tweet> tweetMap;
private static long tweetCount = 0L;
/**
* Main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
Twitter twitter = new Twitter();
twitter.postTweet(1, 5);
twitter.follow(1, 1);
System.out.println(twitter.getNewsFeed(1));
/*twitter.follow(2, 1);
System.out.println(twitter.getNewsFeed(2));
//twitter.unfollow(2, 1);
twitter.postTweet(2, 3);
System.out.println(twitter.getNewsFeed(1));
System.out.println(twitter.getNewsFeed(2));
twitter.follow(1, 2);
System.out.println(twitter.getNewsFeed(1));
twitter.unfollow(2, 1);
System.out.println(twitter.getNewsFeed(2));
System.out.println(twitter.getNewsFeed(1));
//twitter.getNewsFeed(2);
*/
}
/** Initialize your data structure here. */
public Twitter()
{
userMap = new HashMap<>();
tweetMap = new HashMap<>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId)
{
User user = userMap.get(userId);
if(user == null)
{
user = new User(userId);
userMap.put(userId, user);
}
user.addTweet(new Tweet(tweetId, tweetCount++));
}
/** 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)
{
User user = userMap.get(userId);
List<Integer> result = new ArrayList<>();
if(user == null) return result;
Set<Integer> follwers = user.getFollow();
if(follwers != null)
{
List<Tweet> tweets = new ArrayList<>();
tweets.addAll(user.getTweets());
for(Integer i : follwers)
{
User f = userMap.get(i);
if(f != null)
{
tweets.addAll(f.getTweets());
}
}
Comparator<Tweet> comparator = new Comparator<Tweet>() {
@Override
public int compare(Tweet o1, Tweet o2) {
return Long.compare(o2.time, o1.time);
}
};
Collections.sort(tweets, comparator);
for(int i = 0; i < 10; i ++)
{
if(i >= tweets.size())
break;
result.add(tweets.get(i).id);
}
}
return result;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId)
{
if(followerId == followeeId) return;
User user = userMap.get(followerId);
if(user == null)
user = new User(followerId);
userMap.put(followerId, user);
if(userMap.get(followeeId) != null)
{
user.follow(userMap.get(followeeId).id);
}
else
{
User newUser = new User(followeeId);
userMap.put(followeeId, newUser);
user.follow(userMap.get(followeeId).id);
}
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId)
{
User user = userMap.get(followerId);
if(user != null)
{
user.unFollow(followeeId);
}
}
}