c++ 32ms, use max heap for news feed


  • 0
    X
    class Twitter {
    private:
    	class tweetInfo{
    		public:
    			int userId;
    			int tweetId;
    			int time;
    			int idx;
    			tweetInfo(int userId_, int tweetId_, int time_, int idx_) {
    				userId = userId_;
    				tweetId = tweetId_;
    				time = time_;
    				idx = idx_; // index in the vector
    			}
    	};
    	
    	class cmp{
            public:
    		    bool operator()(tweetInfo &t1, tweetInfo &t2) {
    			    return t1.time < t2.time;
    		    }
    	};
        int time;
    	const int NEWSFEEDCOUNT = 10;
    	unordered_map<int, vector<tweetInfo>> allTweet;
    	unordered_map<int, unordered_set<int>> followList;
    	
    public:
        /** Initialize your data structure here. */
        Twitter() {
            time = 0;
        }
        
        /** Compose a new tweet. */
        void postTweet(int userId, int tweetId) {
    		int size = (allTweet.find(userId) == allTweet.end())? 0 : allTweet[userId].size();
            allTweet[userId].push_back(tweetInfo(userId, tweetId, time, size));
    		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) {
            // use priority queue
    		priority_queue<tweetInfo, vector<tweetInfo>, cmp> maxHeap;
    		followList[userId].insert(userId);
    		
    		for (int targetId : followList[userId]) {
    			if (allTweet[targetId].size()) {
    				maxHeap.push(allTweet[targetId].back());
    			}
    		}
    		
    		// extract the tweet Id (at most) 10 times
    		vector<int> result;
    		for (int i = 0; i < NEWSFEEDCOUNT; i++) {
    			if (maxHeap.size() == 0) return result;
    			tweetInfo ti = maxHeap.top();
    			maxHeap.pop();
    			
    			result.push_back(ti.tweetId);
    			int idx = ti.idx;
    			if (idx > 0) {
    				maxHeap.push(allTweet[ti.userId][idx - 1]);
    			}
    		}
    		return result;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        void follow(int followerId, int followeeId) {
            followList[followerId].insert(followeeId);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        void unfollow(int followerId, int followeeId) {
            if (followList[followerId].find(followeeId) != followList[followerId].end()) {
    			followList[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.