import java.util.*;
class Twitter {
private static int timestamp = 0;
// userId 和 User 对象用一个map一一对应
private HashMap<Integer, User> userMap;
private static class Tweet {
private int id;
private int time;
// 指针指向上一条推文
private Tweet next;
// 参数分别代表推文内容和发表时间
public Tweet(int id, int time) {
this.id = id;
this.time = time;
this.next = null;
}
}
private static class User {
private int id;
// 关注列表不可重复 用set存储
public Set<Integer> followed;
// 推文列表用链表存储 便于有序合并
public Tweet head;
public User(int userId) {
followed = new HashSet<>();
this.id = userId;
this.head = null;
// 关注自己
follow(id);
}
public void follow(int userId) {
followed.add(userId);
}
public void unfollow(int userId) {
// 不可取关自己
if (userId != this.id) {
followed.remove(userId);
}
}
public void post(int tweetId) {
Tweet tweet = new Tweet(tweetId, timestamp);
timestamp++;
// 新发表的推文插入推文链表头部
tweet.next = head;
head = tweet;
}
}
/** Initialize your data structure here. */
public Twitter() {
userMap = new HashMap<>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
// 新用户第一次发推 建立 userId 和 User 对象映射关系
if (!userMap.containsKey(userId)) {
userMap.put(userId, new User(userId));
}
User user = userMap.get(userId);
user.post(tweetId);
}
/** 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) {
List<Integer> ans = new ArrayList<>();
// 既没关注任何人 也没发推文 或者不存在
if (!userMap.containsKey(userId)) {
return ans;
}
// 关注列表
Set<Integer> users = userMap.get(userId).followed;
// lambda表达式 -> 通过time降序排列,队列大小为关注列表的大小
PriorityQueue<Tweet> pq = new PriorityQueue<>(users.size(), (a,b) -> (b.time - a.time));
// 将关注列表里所有头节点插入优先级队列
for (int id : users) {
Tweet tweet = userMap.get(id).head;
// 关注的人未发推
if (tweet == null) {
continue;
}
pq.add(tweet);
}
while (!pq.isEmpty()) {
// 最多返回10条
if (ans.size() == 10) {
break;
}
// 取出 time 最大 即最新的推文
Tweet tweet = pq.poll();
ans.add(tweet.id);
// 将下一篇推文插入队列排序
if (tweet.next != null) {
pq.add(tweet.next);
}
}
return ans;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
// follower 第一次关注 建立 followerId 和 User 对象映射关系
if (!userMap.containsKey(followerId)) {
userMap.put(followerId, new User(followerId));
}
// // followee 第一次被关注 建立 followeeId 和 User 对象映射关系
if (!userMap.containsKey(followeeId)) {
userMap.put(followeeId, new User(followeeId));
}
userMap.get(followerId).follow(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if (userMap.containsKey(followerId)) {
userMap.get(followerId).unfollow(followeeId);
}
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/
leetcode-355-设计推特
最新推荐文章于 2024-05-13 21:07:16 发布