C++ solution using the idea of merge sort instead of heap


  • 0
    T
    class Twitter {
    private:
        unordered_map<int, vector<pair<int,int>>> tweets; // tweets[u]: all the tweets that belong to user u, pair(time, tweetId)
        unordered_map<int, unordered_set<int>> followees; // followees[u]: all the users that user u follows
        int timecount;
        
        vector<int> getNewsFeedHelper(vector<vector<pair<int,int>>>& uTweets)
        {
            vector<int> result;
            
            while(result.size()<10)
            {
                int maxTime = INT_MIN;
                
                for(int i=0; i<uTweets.size(); i++)
                    if(uTweets[i].size()>0)
                        maxTime = max(maxTime, uTweets[i].back().first);
                        
                if(maxTime==INT_MIN)
                    break;
                else
                {
                    for(int i=0; i<uTweets.size(); i++)
                        if(uTweets[i].size()>0 && uTweets[i].back().first==maxTime)
                        {
                            if(result.size()==0 || uTweets[i].back().second != result.back())
                                result.push_back(uTweets[i].back().second);
                            uTweets[i].pop_back();
                            break;
                        }
                }
            }
            
            return result;
        }
        
    public:
        /** Initialize your data structure here. */
        Twitter() {
            timecount = 0;
        }
        
        /** Compose a new tweet. */
        void postTweet(int userId, int tweetId) {
            tweets[userId].push_back(make_pair(timecount, tweetId));
            timecount++;
        }
        
        /** 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) {
            vector<vector<pair<int,int>>> uTweets;
            uTweets.push_back(tweets[userId]);
            for(int user : followees[userId])
                uTweets.push_back(tweets[user]);
    
            return getNewsFeedHelper(uTweets);
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        void follow(int followerId, int followeeId) {
            followees[followerId].insert(followeeId);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        void unfollow(int followerId, int followeeId) {
            followees[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.