Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc87bf3

Browse files
Add solution for Design Twitter
1 parent 938c646 commit fc87bf3

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
4545
- Meeting Rooms II | [Problem](https://leetcode.com/problems/meeting-rooms-ii) | [Java Solution](src/javacode/solutions/MeetingRoomsII.java)
4646
- Find Median From Data Stream | [Problem](https://leetcode.com/problems/find-median-from-data-stream) | [Java Solution](src/javacode/solutions/FindMedianFromDataStream.java)
4747
- Stock Price Fluctuation | [Problem](https://leetcode.com/problems/stock-price-fluctuation) | [Java Solution](src/javacode/solutions/StockPrice.java)
48+
- Design Twitter | [Problem](https://leetcode.com/problems/design-twitter) | [Java Solution](src/javacode/solutions/Twitter.java)
4849
- Employee Free Time | [Problem](https://leetcode.com/problems/employee-free-time) | [Java Solution](src/javacode/solutions/EmployeeFreeTime.java)
4950
- Process Tasks Using Servers | [Problem](https://leetcode.com/problems/process-tasks-using-servers) | [Java Solution](src/javacode/solutions/ProcessTasksUsingServers.java)
5051

‎src/javacode/solutions/Twitter.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package javacode.solutions;
2+
3+
import java.util.*;
4+
5+
// [Problem] https://leetcode.com/problems/design-twitter
6+
class Twitter {
7+
Map<Integer, List<Tweet>> tweets;
8+
Map<Integer, Set<Integer>> following;
9+
int timestamp;
10+
11+
// O(n) space
12+
public Twitter() {
13+
tweets = new HashMap<>();
14+
following = new HashMap<>();
15+
timestamp = 0;
16+
}
17+
18+
// O(1) time
19+
public void postTweet(int userId, int tweetId) {
20+
following.putIfAbsent(userId, new HashSet<>());
21+
following.get(userId).add(userId);
22+
tweets.putIfAbsent(userId, new LinkedList<>());
23+
tweets.get(userId).add(new Tweet(tweetId, timestamp++));
24+
}
25+
26+
// O(nlogn) time
27+
public List<Integer> getNewsFeed(int userId) {
28+
Queue<Tweet> newsFeed = new PriorityQueue<>((a, b) -> b.timestamp - a.timestamp);
29+
Set<Integer> followees = following.getOrDefault(userId, new HashSet<>());
30+
for (int followee : followees) {
31+
if (tweets.containsKey(followee)) {
32+
newsFeed.addAll(tweets.get(followee));
33+
}
34+
}
35+
List<Integer> newsFeedTweetIds = new ArrayList<>();
36+
while (!newsFeed.isEmpty() && newsFeedTweetIds.size() < 10) {
37+
newsFeedTweetIds.add(newsFeed.poll().id);
38+
}
39+
return newsFeedTweetIds;
40+
}
41+
42+
// O(1) time
43+
public void follow(int followerId, int followeeId) {
44+
following.putIfAbsent(followerId, new HashSet<>());
45+
following.get(followerId).add(followeeId);
46+
}
47+
48+
// O(1) time
49+
public void unfollow(int followerId, int followeeId) {
50+
following.get(followerId).remove(followeeId);
51+
}
52+
53+
public static void main(String[] args) {
54+
Twitter twitter = new Twitter();
55+
twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5).
56+
System.out.println(twitter.getNewsFeed(1)); // User 1's news feed should return a list with 1 tweet id -> [5]. return [5]
57+
twitter.follow(1, 2); // User 1 follows user 2.
58+
twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6).
59+
System.out.println(twitter.getNewsFeed(1)); // 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.
60+
twitter.unfollow(1, 2); // User 1 unfollows user 2.
61+
System.out.println(twitter.getNewsFeed(1)); // User 1's news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2.
62+
}
63+
}
64+
65+
class Tweet {
66+
int id;
67+
int timestamp;
68+
69+
public Tweet(int id, int timestamp) {
70+
this.id = id;
71+
this.timestamp = timestamp;
72+
}
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /