Java solution with HashMap and self-defined class


  • 0
    Y
    public class Twitter {
    
    private class Post {
        Integer id;
        Integer time;
        
        Post(int id, int time) {
            this.id = id;
            this.time = time;
        }
    }
    int timestamp; // unique identifier of posts
    Map<Integer, Set<Integer>> friends;
    Map<Integer, List<Post>> posts;
    /** Initialize your data structure here. */
    public Twitter() {
        timestamp = 0;
        friends = new HashMap<>();
        posts = new HashMap<>();
    }
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if (posts.get(userId) == null) {
            posts.put(userId, new ArrayList<Post>());
        }
        posts.get(userId).add(new Post(tweetId, timestamp++));
    }
    
    /** 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<Post> tmp = new ArrayList<>();
        if (posts.get(userId) != null) {
            tmp.addAll(posts.get(userId));
        }
        if (friends.get(userId) != null) {
            for (Integer id : friends.get(userId)) {
                if (posts.get(id) != null) {
                    tmp.addAll(posts.get(id));
                }
            }
        }
        Collections.sort(tmp, new Comparator<Post>() { // reversely sort based on timestamp
            public int compare(Post p1, Post p2) {
                return p2.time - p1.time;
            }
        });
        if (tmp.size() > 10) {
            tmp = tmp.subList(0, 10);
        }
        List<Integer> result = new ArrayList<>();
        for (Post p : tmp) {
            result.add(p.id);
        }
        return result;
    }
    
    /** 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;  // cannot befriend oneself
        if (friends.get(followerId) == null) {
            friends.put(followerId, new HashSet<>());
        }
        friends.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 (friends.get(followerId) != null) {
            friends.get(followerId).remove(followeeId);
        }
    }
    

    }


Log in to reply
 

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