My WA C++ code.


  • 0
    R
    unordered_map<int, unordered_set<int>> following;
    unordered_map<int, vector<pair<int, int>>> waiting;
    int C;
    
    Twitter() {
        
    }
    
    void postTweet(int userId, int tweetId) {
        
        if(waiting[userId].size() == 10)
            waiting[userId].erase(waiting[userId].begin());
        waiting[userId].push_back({C++, tweetId});
        
    }
    
    vector<int> getNewsFeed(int userId) {
        
        vector<int> rez;
        
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        
        for(unordered_set<int>::iterator it = following[userId].begin(); it != following[userId].end(); it++) {
         
            for(vector<pair<int, int>>::iterator it2 = waiting[*it].begin(); it2 != waiting[*it].end(); it2++) {
                
                if(pq.size() == 10)
                    pq.pop();
                pq.push(*it2);
                
            }
            
        }
        
        for(vector<pair<int, int>>::iterator it2 = waiting[userId].begin(); it2 != waiting[userId].end(); it2++) {
                
            if(pq.size() == 10)
                pq.pop();
            pq.push(*it2);
                
        }
        
        while(!pq.empty()) {
            rez.push_back(pq.top().second);
            pq.pop();
        }
        
        reverse(rez.begin(), rez.end());
        
        return rez;
    }
    
    void follow(int followerId, int followeeId) {
        if(followerId == followeeId)
            return;
        following[followerId].insert(followeeId);
    }
    
    void unfollow(int followerId, int followeeId) {
        following[followerId].erase(followeeId);
    }
    };
    

    I am getting WA on test 19/21 and I've been staring at the output/expected and they look exactly the same to me.

    Can someone please help me debug my code or tell me why I'm getting WA while my answer is identical to the expected one?


Log in to reply
 

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