yokostan / Leetcode-Solutions

Doing exercise on Leetcode. Carry on!
0 stars 3 forks source link

Leetcode #355. Design Twitter #275

Open yokostan opened 5 years ago

yokostan commented 5 years ago
class Twitter {
    private int timeStamp = 0;
    class Tweet {
        public int id;
        public int time;
        public Tweet next;
        Tweet(int id) {
            this.id = id;
            time = timeStamp++;
            next = null;
        }
    }

    class User {
        public int id;
        public Set<Integer> followed;
        public Tweet tweet_head;
        User(int id) {
            this.id = id;
            followed = new HashSet<>();
            followed.add(id);
        }

        public void post(int id) {
            Tweet t = new Tweet(id);
            t.next = tweet_head;
            tweet_head = t;
        }
    }

    public HashMap<Integer, User> map;

    /** Initialize your data structure here. */
    public Twitter() {
        this.map = new HashMap<>();
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if (!map.containsKey(userId)) {
            map.put(userId, new User(userId));
        }
        map.get(userId).post(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) {
        List<Integer> res = new ArrayList<Integer>();
        if (!map.containsKey(userId)) return res;
        Set<Integer> users = map.get(userId).followed;
        PriorityQueue<Tweet> q = new PriorityQueue<Tweet>(users.size(), (a,b) -> (b.time - a.time));
        for (int user : users) {
            Tweet t = map.get(user).tweet_head;
            if (t != null) {
                q.add(t);
            }
        }
        int count = 0;
        while (!q.isEmpty() && count < 10) {
            Tweet t = q.poll();
            res.add(t.id);
            count++;
            if (t.next != null) {
                q.add(t.next);
            }
        }
        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)) {
            User u = new User(followerId);
            map.put(followerId, u);
        }
        if (!map.containsKey(followeeId)) {
            User u = new User(followeeId);
            map.put(followeeId, u);
        }
        map.get(followerId).followed.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 (!map.containsKey(followerId) || followerId == followeeId) return ;
        map.get(followerId).followed.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);
 */