C++ solution using priority_queue, 72ms


  • 0
    J

    Inspired by this post: https://discuss.leetcode.com/topic/48253/72ms-c-solution

    The priority queue tracks the follower and all his/her followees' latest tweets, not all of them, this is necessary because the global 10 latest tweets must be from them. And after getting one global latest tweet post by one user( i.e. user A ) from the queue, fill A's second latest tweet into the queue if A has one.

    class Twitter {
        class twitte {
            friend class Twitter;
            friend class Compare;
            int tid;
            int time;
        public:
            twitte(int tid, int time): tid(tid), time(time) {}
        };
        
        class Compare {
        public:
            bool operator()(const pair<twitte*, twitte*>& x, const pair<twitte*, twitte*>& y) {
                return (x.second->time < y.second->time);
            }
        };
        
        int time;
        unordered_map<int, vector<twitte>> twittes;
        unordered_map<int, unordered_set<int>> follows;
        
    public:
        /** Initialize your data structure here. */
        Twitter(): time(0) {}
        
        /** Compose a new tweet. */
        void postTweet(int userId, int tweetId) {
            twittes[userId].emplace_back(tweetId, time++);    
        }
        
        /** 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. */
        vector<int> getNewsFeed(int userId) {
            priority_queue<pair<twitte *, twitte *>, vector<pair<twitte *, twitte *>>, Compare> pt;
            for(auto followee : follows[userId]) {
                auto &t = twittes[followee];
                if(t.size() > 0)
                    pt.push(make_pair(t.data(), t.data() + t.size() - 1));
            }
            auto &t = twittes[userId];
            if(t.size() > 0)
                pt.push(make_pair(t.data(), t.data() + t.size() - 1));
            vector<int> res;
            const int n = 10;
            for(int i = 0; i < n && !pt.empty(); i++) {
                auto p = pt.top();
                pt.pop();
                res.push_back(p.second->tid);
                if(p.second-- > p.first)
                    pt.push(p);
            }
            return res;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        void follow(int followerId, int followeeId) {
            if(followerId != followeeId)
                follows[followerId].insert(followeeId);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        void unfollow(int followerId, int followeeId) {
            follows[followerId].erase(followeeId);
        }
    };
    
    /**
     * Your Twitter object will be instantiated and called as such:
     * Twitter obj = new Twitter();
     * obj.postTweet(userId,tweetId);
     * vector<int> param_2 = obj.getNewsFeed(userId);
     * obj.follow(followerId,followeeId);
     * obj.unfollow(followerId,followeeId);
     */
    

Log in to reply
 

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