zoukankan      html  css  js  c++  java
  • [LeetCode] 355. 设计推特

    不得不说 真的很强 根本想不到

    package leetcode;
    
    import java.util.*;
    
    /**
     * @author doyinana
     * @create 2020-04-14 0:29
     */
    public class Twitter {
        public static int timestamp=0;
    
        public class Tweet{
            public int tweetId;
            public int time;
            public Tweet next;
            public Tweet(int tweetId ,int time){
                this.tweetId = tweetId;
                this.time = time;
                this.next = null;
            }
        }
    
        public class User{
            private int userId;
            public Set<Integer> followed;
            public Tweet headTweet;
    
            public User(int userId){
                followed = new HashSet<>();
                this.userId = userId;
                this.headTweet = null;
                //用户关注一下自己
                follow(this.userId);
            }
    
            public void follow(int userId) {
                followed.add(userId);
            }
    
            public void unfollow(int userId) {
                // 不可以取关自己
                if (userId != this.userId)
                    followed.remove(userId);
            }
    
            public void post(int tweetId){
                Tweet twt = new Tweet(tweetId,timestamp);
                timestamp++;
                twt.next=headTweet;
                headTweet=twt;
            }
        }
    
        private HashMap<Integer, User> userHashMap = new HashMap<>();
    
        public void postTweet(int userId,int tweetId){
            if(!userHashMap.containsKey(userId)){
                userHashMap.put(userId,new User(userId));
            }
            userHashMap.get(userId).post(tweetId);
        }
    
        public List<Integer> getNewsFeed(int userId){
            List<Integer> result=new ArrayList<>();
            if(!userHashMap.containsKey(userId)){
                return result;
            }
            Set<Integer> users=userHashMap.get(userId).followed;
            PriorityQueue<Tweet> priorityQueue=new PriorityQueue<>(users.size(),(a,b)->
                    (b.time-a.time));
            for(int id:users){
                Tweet tweet=userHashMap.get(id).headTweet;
                if(tweet==null) continue;
                priorityQueue.add(tweet);
            }
            while(!priorityQueue.isEmpty()){
                if(result.size()==10){
                    break;
                }
                Tweet tweet=priorityQueue.poll();
                result.add(tweet.tweetId);
                if(tweet.next!=null){
                    priorityQueue.add(tweet.next);
                }
            }
            return result;
        }
    
        public void follow(int followerId,int followeeId){
            if(!userHashMap.containsKey(followerId)){
                userHashMap.put(followerId,new User(followerId));
            }
            if (!userHashMap.containsKey(followeeId)) {
                userHashMap.put(followeeId, new User(followeeId));
            }
            userHashMap.get(followerId).follow(followeeId);
        }
    
        public void unfollow(int followerId, int followeeId) {
            if (userHashMap.containsKey(followerId)) {
                userHashMap.get(followerId).unfollow(followeeId);
            }
        }
    }

  • 相关阅读:
    3.3 直方图处理与函数绘图
    光头强
    考试代码模板
    【2015初赛】预备
    NOIP2018 模拟题
    NOIP2017 模拟赛
    【解题报告】树形DP入门
    【解题报告】区间DP
    【解题报告】树形背包
    二分刷题单
  • 原文地址:https://www.cnblogs.com/doyi111/p/12695329.html
Copyright © 2011-2022 走看看