Super easy to understand solution in Java. OOP, HashMap, PriorityQueue


  • 0
    M
    public class Twitter {
        static int timeStamp = 0;
        Map<Integer,User> map;
    
        private class Tweet{
            public int id;
            public int time;
            public Tweet next;
            public Tweet(int tweetId){
                this.id = tweetId;
                this.time = timeStamp++;
                next = null;
            }
        }
        
        public class User{
            public int id;
            public Set<User> following;
            public Tweet tweet;
            
            public User(int id){
                this.id = id;
                following = new HashSet<User>();
                following.add(this);
                tweet = null;
            }
            public void tweet(int tweetId){
                Tweet new_tweet = new Tweet(tweetId);
                new_tweet.next = tweet; 
                tweet = new_tweet;
            }
            public void follow(int anotherId){
                if(!map.containsKey(anotherId)){
                    return;
                }
                following.add(map.get(anotherId));
            }
            public void unfollow(int anotherId){
                if(!map.containsKey(anotherId)){
                    return;
                }
                following.remove(map.get(anotherId));
            }
        }
        /** Initialize your data structure here. */
        public Twitter() {
            map = new HashMap<Integer,User>();
        }
        
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) {
            if(!map.containsKey(userId)){
                map.put(userId,new User(userId));
            }
            map.get(userId).tweet(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) {
            User user = map.get(userId);
            List<Integer> res = new LinkedList<Integer>();
            if(user == null){
                return res; 
            }
            PriorityQueue<Tweet> queue = new PriorityQueue<Tweet>(new Comparator<Tweet>(){
                public int compare(Tweet t1, Tweet t2){
                    return t2.time - t1.time;
                }
            });
            for(User person : user.following){
                Tweet t = person.tweet;
                if(t != null){
                    queue.offer(t);            
                }
            }
            for(int i = 0; i < 10; i++){
                if(!queue.isEmpty()){
                    Tweet t = queue.poll();
                    res.add(t.id);
                    t = t.next;
                    if(t != null){
                        queue.offer(t);
                    }
                }else{
                    break;
                }
            }
            return res;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(int followerId, int followeeId) {
            if(!map.containsKey(followerId)){
                map.put(followerId,new User(followerId));
            }
            if(!map.containsKey(followeeId)){
                map.put(followeeId,new User(followeeId));
            }
            map.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(followerId == followeeId){
                return;
            }
            if(!map.containsKey(followerId)){
                map.put(followerId,new User(followerId));
            }
            if(!map.containsKey(followeeId)){
                map.put(followeeId,new User(followeeId));
            }
            map.get(followerId).unfollow(followeeId);
        }
    }

Log in to reply
 

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