You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
82
+
"""
83
+
following =self.user_following[userId]
84
+
users =set(following)
85
+
users.add(userId)
86
+
tweets = [self.user_tweets[u][::-1][:10] for u in users]
/** 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. */
142
+
publicList<Integer>getNewsFeed(intuserId) {
143
+
Set<Integer> following = userFollowing.getOrDefault(userId, newHashSet<>());
144
+
Set<Integer> users =newHashSet<>(following);
145
+
users.add(userId);
146
+
PriorityQueue<Integer> pq =newPriorityQueue<>(10, (a, b) -> (tweets.get(b) - tweets.get(a)));
147
+
for (Integer u : users) {
148
+
List<Integer> userTweet = userTweets.get(u);
149
+
if (userTweet !=null&&!userTweet.isEmpty()) {
150
+
for (int i = userTweet.size() -1, k =10; i >=0&& k >0; --i, --k) {
151
+
pq.offer(userTweet.get(i));
152
+
}
153
+
}
154
+
}
155
+
List<Integer> res =newArrayList<>();
156
+
while (!pq.isEmpty() && res.size() <10) {
157
+
res.add(pq.poll());
158
+
}
159
+
return res;
160
+
}
161
+
162
+
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
163
+
publicvoidfollow(intfollowerId, intfolloweeId) {
164
+
Set<Integer> following = userFollowing.getOrDefault(followerId, newHashSet<>());
165
+
following.add(followeeId);
166
+
userFollowing.put(followerId, following);
167
+
}
168
+
169
+
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
Copy file name to clipboardExpand all lines: solution/0300-0399/0355.Design Twitter/README_EN.md
+118-3
Original file line number
Diff line number
Diff line change
@@ -47,21 +47,136 @@ twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 t
47
47
<li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>postTweet</code>, <code>getNewsFeed</code>, <code>follow</code>, and <code>unfollow</code>.</li>
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.
79
+
"""
80
+
following =self.user_following[userId]
81
+
users =set(following)
82
+
users.add(userId)
83
+
tweets = [self.user_tweets[u][::-1][:10] for u in users]
/** 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. */
137
+
publicList<Integer>getNewsFeed(intuserId) {
138
+
Set<Integer> following = userFollowing.getOrDefault(userId, newHashSet<>());
139
+
Set<Integer> users =newHashSet<>(following);
140
+
users.add(userId);
141
+
PriorityQueue<Integer> pq =newPriorityQueue<>(10, (a, b) -> (tweets.get(b) - tweets.get(a)));
142
+
for (Integer u : users) {
143
+
List<Integer> userTweet = userTweets.get(u);
144
+
if (userTweet !=null&&!userTweet.isEmpty()) {
145
+
for (int i = userTweet.size() -1, k =10; i >=0&& k >0; --i, --k) {
146
+
pq.offer(userTweet.get(i));
147
+
}
148
+
}
149
+
}
150
+
List<Integer> res =newArrayList<>();
151
+
while (!pq.isEmpty() && res.size() <10) {
152
+
res.add(pq.poll());
153
+
}
154
+
return res;
155
+
}
156
+
157
+
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
158
+
publicvoidfollow(intfollowerId, intfolloweeId) {
159
+
Set<Integer> following = userFollowing.getOrDefault(followerId, newHashSet<>());
160
+
following.add(followeeId);
161
+
userFollowing.put(followerId, following);
162
+
}
163
+
164
+
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
0 commit comments