-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignTwitter.java
More file actions
109 lines (90 loc) · 3.56 KB
/
DesignTwitter.java
File metadata and controls
109 lines (90 loc) · 3.56 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
/*
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.
Implement the Twitter class:
Twitter() Initializes your twitter object.
void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user userId. Each call to this function will be made with a unique tweetId.
List<Integer> getNewsFeed(int userId) Retrieves 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 themself. Tweets must be ordered from most recent to least recent.
void follow(int followerId, int followeeId) The user with ID followerId started following the user with ID followeeId.
void unfollow(int followerId, int followeeId) The user with ID followerId started unfollowing the user with ID followeeId.
Example 1:
Input
["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"]
[[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
Output
[null, null, [5], null, null, [6, 5], null, [5]]
*/
class Tweet implements Comparable<Tweet> {
int time;
int tweetId;
Tweet(int time, int tweetId) {
this.time = time;
this.tweetId = tweetId;
}
public int compareTo(Tweet that) {
return that.time - this.time; // Max-heap: latest tweets come first
}
}
class User {
int userId;
HashSet<Integer> followees;
List<Tweet> tweets;
User(int userId) {
this.userId = userId;
followees = new HashSet<>();
tweets = new LinkedList<>();
followees.add(userId); // follow self
}
void addTweet(Tweet t) {
tweets.add(0, t); // insert at head to keep latest tweets at front
}
void follow(int followeeId) {
followees.add(followeeId);
}
void unfollow(int followeeId) {
if (followeeId != this.userId) { // can't unfollow self
followees.remove(followeeId);
}
}
}
class Twitter {
HashMap<Integer, User> userMap;
int timeStamp;
public Twitter() {
userMap = new HashMap<>();
timeStamp = 0;
}
public void postTweet(int userId, int tweetId) {
userMap.putIfAbsent(userId, new User(userId));
User user = userMap.get(userId);
user.addTweet(new Tweet(timeStamp++, tweetId));
}
public List<Integer> getNewsFeed(int userId) {
if (!userMap.containsKey(userId)) return new ArrayList<>();
PriorityQueue<Tweet> pq = new PriorityQueue<>();
User user = userMap.get(userId);
for (int followeeId : user.followees) {
User followee = userMap.get(followeeId);
if (followee == null) continue;
List<Tweet> tweets = followee.tweets;
for (int i = 0; i < Math.min(10, tweets.size()); i++) {
pq.offer(tweets.get(i));
}
}
List<Integer> result = new ArrayList<>();
int count = 0;
while (!pq.isEmpty() && count < 10) {
result.add(pq.poll().tweetId);
count++;
}
return result;
}
public void follow(int followerId, int followeeId) {
userMap.putIfAbsent(followerId, new User(followerId));
userMap.putIfAbsent(followeeId, new User(followeeId));
userMap.get(followerId).follow(followeeId);
}
public void unfollow(int followerId, int followeeId) {
if (!userMap.containsKey(followerId) || !userMap.containsKey(followeeId)) return;
userMap.get(followerId).unfollow(followeeId);
}
}