nord vpnnord vpn
Ad

Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
569 B
1
Indexable
Never
class Solution { //priority queue
public:
    vector<int> topKFrequent(vector<int>& arr, int k) {
        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
        unordered_map<int,int> fr;

        for(auto x:arr) fr[x]++;

        for(auto m:fr){
            pq.push({m.second,m.first}); //frequency , elemnt
            if(pq.size()>k) pq.pop();
        }

        vector<int> res;
        while(!pq.empty()){
            res.push_back(pq.top().second);
            pq.pop();
        }
        return res;
    }
};

nord vpnnord vpn
Ad