g0301_0400.s0355_design_twitter.Twitter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0301_0400.s0355_design_twitter;
// #Medium #Hash_Table #Design #Heap_Priority_Queue #Linked_List
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class Twitter {
// userId --> user followers
private Map> twitterData;
// head of linked list that stores all the tweets
private Tweet head;
public Twitter() {
head = new Tweet(-1, -1);
twitterData = new HashMap<>();
}
public void postTweet(int userId, int tweetId) {
checkNewUser(userId);
Tweet t = new Tweet(userId, tweetId);
Tweet next = head.next;
head.next = t;
t.next = next;
}
public List getNewsFeed(int userId) {
checkNewUser(userId);
List res = new ArrayList<>();
HashSet followers = twitterData.get(userId);
Tweet t = head.next;
while (t != null && res.size() < 10) {
if (followers.contains(t.userId)) {
res.add(t.tweetId);
}
t = t.next;
}
return res;
}
public void follow(int followerId, int followeeId) {
checkNewUser(followeeId);
checkNewUser(followerId);
twitterData.get(followerId).add(followeeId);
}
public void unfollow(int followerId, int followeeId) {
checkNewUser(followeeId);
// cannot unfollower yourself
if (followerId == followeeId) {
return;
}
checkNewUser(followerId);
twitterData.get(followerId).remove(followeeId);
}
public void checkNewUser(int userId) {
if (twitterData.containsKey(userId)) {
return;
}
twitterData.put(userId, new HashSet<>());
// follow yourself
twitterData.get(userId).add(userId);
}
}