one hashmap + one priorityQueue java solution


  • 0
    C

    class Twitter {
    class User {
    int uid;
    List<Tweet> tweets;
    HashSet<Integer> followee;
    public User(int uid) {
    this.uid = uid;
    this.followee = new HashSet<>();
    this.tweets = new ArrayList<>();
    };
    }
    class Tweet {
    int tid;
    int timestamp;
    public Tweet(int tid) {
    this.tid = tid;
    };
    }
    HashMap<Integer, User> map;
    int curTime;
    /** Initialize your data structure here. */
    public Twitter() {
    map = new HashMap<>();
    curTime = 0;
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(!map.containsKey(userId)) {
            User user = new User(userId);
            Tweet tweet = new Tweet(tweetId);
            tweet.timestamp = curTime;
            user.tweets.add(tweet);
            map.put(userId, user);
        } else {
            Tweet tweet = new Tweet(tweetId);
            tweet.timestamp = curTime;
            map.get(userId).tweets.add(tweet);
        }
        curTime++;
    }
    
    /** 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) {
        PriorityQueue<Tweet> pq = new PriorityQueue<>((a, b) -> b.timestamp - a.timestamp);
        List<Integer> res = new ArrayList<>();
        if(map.containsKey(userId)) {
            for(Tweet tw : map.get(userId).tweets)
                pq.offer(tw);
            for(Integer fid : map.get(userId).followee) {
                if(map.containsKey(fid) && fid != userId) {
                    for(Tweet tw : map.get(fid).tweets)
                        pq.offer(tw);
                }
            }
            int i = 0;
            while(!pq.isEmpty() && i < 10) {
                Tweet cur = pq.poll();
                res.add(cur.tid);
                i++;
            }
        }
        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.get(followerId).followee.add(followeeId);
        } else {
            User user = new User(followerId);
            map.put(followerId, user);
            map.get(followerId).followee.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 && map.containsKey(followerId)) {
            map.get(followerId).followee.remove(followeeId);
        }
    }
    

    }


  • 0
    C
    public void postTweet(int userId, int tweetId) {
        map.putIfAbsent(userId, new User(userId));
        Tweet tweet = new Tweet(tweetId);
        tweet.timestamp = curTime;
        map.get(userId).tweets.add(tweet);
        curTime++;
    }
    
    /** 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) {
        PriorityQueue<Tweet> pq = new PriorityQueue<>((a, b) -> b.timestamp - a.timestamp);
        List<Integer> res = new ArrayList<>();
        if(map.containsKey(userId)) {
            for(Tweet tw : map.get(userId).tweets)
                pq.offer(tw);
            for(Integer fid : map.get(userId).followee) {
                if(map.containsKey(fid) && fid != userId) {
                    for(Tweet tw : map.get(fid).tweets)
                        pq.offer(tw);
                }
            }
            int i = 0;
            while(!pq.isEmpty() && i < 10) {
                Tweet cur = pq.poll();
                res.add(cur.tid);
                i++;
            }
        }
        return res;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        map.putIfAbsent(followerId, new User(followerId));
        map.get(followerId).followee.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 && map.containsKey(followerId)) {
            map.get(followerId).followee.remove(followeeId);
        }
    }
    

    revise some redudant code


Log in to reply
 

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