c++ solution with hashtable and priority queue beats 99%


  • 0
    K
    class Twitter {
        int time;
        struct tweet{
            int id;
            int time;
            
            tweet(int time, int id):time(time), id(id){
            }
            
        };
        
        unordered_map<int, unordered_set<int>> userfollow;
        unordered_map<int, vector<tweet>> usertweet;
        
        struct compare{
            bool operator()(const pair<tweet*, tweet*>& a, const pair<tweet*, tweet*>& b)
            {
                return a.second->time < b.second->time;
            }
        };
        
    public:
        /** Initialize your data structure here. */
        Twitter() {
            time = 0;
        }
        
        /** Compose a new tweet. */
        void postTweet(int userId, int tweetId) {
            tweet newtweet(time, tweetId);
            usertweet[userId].push_back(newtweet);
            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) {
            vector<int> res;
            priority_queue<pair<tweet*, tweet*>, vector<pair<tweet*, tweet*>>, compare> pq;
            for(auto user : userfollow[userId])
            {
                if(usertweet.find(user) != usertweet.end() && user != userId)
                {
                    pq.push(make_pair(usertweet[user].data() - 1, usertweet[user].data() + usertweet[user].size() - 1));
                }
            }
            if(usertweet.find(userId) != usertweet.end())
                pq.push(make_pair(usertweet[userId].data() - 1, usertweet[userId].data() + usertweet[userId].size() - 1));
            
            int n = 10;
            while(!pq.empty() && n)
            {
                auto front = pq.top();
                pq.pop();
                res.push_back(front.second->id);
                front.second--;
                n--;
                if(front.first != front.second)
                {
                    pq.push(make_pair(front.first, front.second));
                }
            }
            
            return res;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        void follow(int followerId, int followeeId) {
            userfollow[followerId].insert(followeeId);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        void unfollow(int followerId, int followeeId) {
            userfollow[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.