42ms c++ - use min heap


  • 0
    S
    class Twitter {
    public:
        /** Initialize your data structure here. */
        Twitter() : _tweetNum(0) {
            
        }
        
        /** Compose a new tweet. */
        void postTweet(int userId, int tweetId) {
            _userMap[userId]._tweets.push_back(make_pair(++_tweetNum, 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. */
        vector<int> getNewsFeed(int userId) {
            if(!_userMap.count(userId))
                return {};
            
            vector<int> res;
            priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
            insertIntoPQ(userId, pq);
            for(auto it = _userMap[userId]._followees.begin();
                it != _userMap[userId]._followees.end();
                ++it)
            {
                insertIntoPQ(*it, pq);
            }
            
            while(!pq.empty())
            {
                res.push_back(pq.top().second);
                pq.pop();
            }
            
            reverse(res.begin(), res.end());
            return res;
        }
        
        void insertIntoPQ(int userId,
                          priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>& pq)
        {
            unsigned num = 10;
            for(auto rit = _userMap[userId]._tweets.rbegin();
                rit != _userMap[userId]._tweets.rend();
                ++rit)
            {
                if(!num)
                    return;
                
                if(pq.size() == 10)
                {
                    if(pq.top().first < rit->first)
                    {
                        pq.pop();
                        pq.push(*rit);
                    }
                }
                else
                {
                    pq.push(*rit);
                }
                --num;
            }        
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        void follow(int followerId, int followeeId) {
            if(followerId == followeeId)
                return;
    
            _userMap[followerId]._followees.insert(followeeId);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        void unfollow(int followerId, int followeeId) {
            if(followerId == followeeId)
                return;
    
            _userMap[followerId]._followees.erase(followeeId);
        }
    
    private:
        struct userInfo
        {
            list<pair<int, int>> _tweets;
            set<int> _followees;
        };
        unordered_map<int, userInfo> _userMap;
        unsigned long _tweetNum;
    };
    
    

  • 0
    S

    Complexity of the getNewsFeed is knlogk where k is the number of most recent tweet ids that need to be returned by getNewsFeed. (For k = 10, it is O(n))


Log in to reply
 

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