leetcode 355 设计推特

用链表存储用户发送的每一个推特,用堆获取最先的10条动态

java 复制代码
class Twitter {
    Map<Integer,Set<Integer>> followMap;
    //规定最新的放到最后
    Map<Integer,Tweet> postMap;
    //优先队列(堆)
    PriorityQueue<Tweet> priorityQueue;
    int timeStamp = 0;
    int limit = 10;
    public Twitter() {
        followMap = new HashMap();
        postMap = new HashMap<>();
        //按照每一个推特发送的时间戳由大到小排布
        priorityQueue = new PriorityQueue<>((t1,t2) -> t2.timeStamp - t1.timeStamp);
    }
    //userId发送推特
    public void postTweet(int userId, int tweetId) {
        //首先根据postMap来获取userId对应发送到文章
        Tweet tweet = postMap.get(userId);
        //生成新的tweet
        Tweet newTweet = new Tweet(tweetId, timeStamp++, tweet);
        postMap.put(userId,newTweet);
    }
    //根据userId获得自己和关注用户的10条推特,按时间顺序由近到远排序
    public List<Integer> getNewsFeed(int userId) {
        //因为每一个用户都有自己的优先队列,所以先清空优先队列
        priorityQueue.clear();
        //将自己和关注的用户发送的最新的推特id先放入到优先队列
        if (postMap.containsKey(userId))
            priorityQueue.offer(postMap.get(userId));
        Set<Integer> follows = followMap.get(userId);
        if (follows != null){
            for (Integer follow : follows) {
                 if (postMap.containsKey(follow))
                priorityQueue.offer(postMap.get(follow));
            }
        }
        //现在用户和所有关注的推特都已经放入到优先队列,开始获取前10条
        int count = 0;
        ArrayList<Integer> result = new ArrayList<>();
        while (!priorityQueue.isEmpty() && count < limit){
            //获取头部,在优先队列中删除
            Tweet tweet = priorityQueue.poll();
            result.add(tweet.id);
            if (tweet.next != null)
                priorityQueue.offer(tweet.next);
            count++;
        }
        return result;
    }
    //关注
    public void follow(int followerId, int followeeId) {
        // 被关注人不能是自己
        if (followeeId == followerId) {
            return;
        }
        
        Set<Integer> follows = followMap.getOrDefault(followerId, new HashSet<>());
        follows.add(followeeId);
        followMap.put(followerId,follows);
    }
    //取关
    public void unfollow(int followerId, int followeeId) {
        // 被关注人不能是自己
        if (followeeId == followerId) {
            return;
        }
        Set<Integer> follows = followMap.getOrDefault(followerId, new HashSet<>());
        follows.remove(followeeId);
        followMap.put(followerId,follows);
    }
}
class Tweet{
    int id;
    int timeStamp;
    Tweet next;

    public Tweet(int id, int timeStamp) {
        this.id = id;
        this.timeStamp = timeStamp;
    }

    public Tweet(int id, int timeStamp, Tweet next) {
        this.id = id;
        this.timeStamp = timeStamp;
        this.next = next;
    }
}

/**
 * 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);
 */
相关推荐
hsling松子2 小时前
使用PaddleHub智能生成,献上浓情国庆福
人工智能·算法·机器学习·语言模型·paddlepaddle
dengqingrui1232 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
C++忠实粉丝2 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
ZZZ_O^O3 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
CV-King3 小时前
opencv实战项目(三十):使用傅里叶变换进行图像边缘检测
人工智能·opencv·算法·计算机视觉
代码雕刻家4 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain4 小时前
算法 | 位运算(哈希思想)
算法
Kalika0-05 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
sp_fyf_20246 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
我是哈哈hh7 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝