Easy to Understand Java Code


  • 0
    
    class Tweet {
        public int tweetId;
        public int time;
        
        public Tweet(int tweetid, int time) {
            this.tweetId = tweetid;
            this.time = time;
        }
    }
    
    
    public class Twitter {
        
        private HashMap<Integer, Set<Integer>> friends;
        private HashMap<Integer, List<Tweet>> tweetMap;
        private PriorityQueue<Tweet> tqueue;
        private static int time = 0;
        
        /** Initialize your data structure here. */
        public Twitter() {
            this.friends = new HashMap();
            this.tweetMap = new HashMap();
            this.tqueue = new PriorityQueue<Tweet>(10, new Comparator<Tweet>(){
                public int compare(Tweet a, Tweet b) {
                    return b.time - a.time;
                }
            });
        }
        
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) {
            time++;
            Tweet currTweet = new Tweet(tweetId, time);
            if(tweetMap.containsKey(userId)) {
                tweetMap.get(userId).add(currTweet);
            }
            else {
                List<Tweet> list = new ArrayList();
                list.add(currTweet);
                tweetMap.put(userId, list);
            }
        }
        
        /** 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> retList = new LinkedList();
            
            //get for self
            if(tweetMap.containsKey(userId)) {
                for(Tweet a : tweetMap.get(userId)) {
                    tqueue.offer(a);
                }
            }
            
            //get for friends
            if(friends.containsKey(userId)) {
                for(int id : friends.get(userId)) {
                    if(tweetMap.containsKey(id)) {
                        for(Tweet a : tweetMap.get(id)) {
                            tqueue.offer(a);
                        }
                    }
                }
            }
            
            int i = 10;
            
            while(i > 0 && !tqueue.isEmpty()) {
                retList.add(tqueue.poll().tweetId);
                i--;
            }
            
            tqueue.clear();
            
            return retList;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(int followerId, int followeeId) {
            if(followerId != followeeId) {
                if(friends.containsKey(followerId)) {
                    friends.get(followerId).add(followeeId);
                }
                else {
                    Set<Integer> set = new HashSet();
                    //set.add(followerId);
                    set.add(followeeId);
                    friends.put(followerId, set);
                }
            }
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        public void unfollow(int followerId, int followeeId) {
            if(friends.containsKey(followerId)) {
                friends.get(followerId).remove(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);
     */
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.