A java solution similar to Merging K Sorted List


  • 0
    L
    //This solution avoids all posts being added into the priority queue.
    public class Twitter {
        private class Post{
            public int tweetId;
            public int sequence;
            public Post next;
            public Post(int id, int seq, Post nxt){
                tweetId = id;
                sequence = seq;
                next = nxt;
            }
        }
        private class Com implements Comparator<Post>{
            public int compare(Post p1, Post p2){
                return -(p1.sequence - p2.sequence);
            }
        }
        private int seq;
        private Map<Integer, Set<Integer>> fls;
        private Map<Integer, Post> posts;
        /** Initialize your data structure here. */
        public Twitter() {
            fls = new HashMap<Integer, Set<Integer>>();
            posts = new HashMap<Integer, Post>();
            seq = 0;
        }
        
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) {
            if(!posts.containsKey(userId)){
                posts.put(userId, null);
            }
            if(!fls.containsKey(userId)){
                fls.put(userId, new HashSet<Integer>());
            }
            posts.put(userId, new Post(tweetId, seq++, posts.get(userId)));
        }
        
        /** 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> list = new ArrayList<Integer>();
            if(!fls.containsKey(userId)) return list;
            PriorityQueue<Post> set = new PriorityQueue<Post>(new Com());
            if(posts.containsKey(userId)){
                set.add(posts.get(userId));
            }
            for(int id: fls.get(userId)){
                if(posts.containsKey(id)){
                    set.add(posts.get(id));
                }
            }
            for(int i = 0; i < 10 && !set.isEmpty(); i++){
                Post tmp = set.poll();
                list.add(tmp.tweetId);
                if(tmp.next != null) set.add(tmp.next);
            }
            return list;
        }
        
        /** 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) return;
            if(!fls.containsKey(followerId)) fls.put(followerId, new HashSet<Integer>());
            fls.get(followerId).add(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(!fls.containsKey(followerId)) return;
            fls.get(followerId).remove(followeeId);
        }
    }

Log in to reply
 

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