Skip to content

Latest commit

 

History

History
35 lines (26 loc) · 731 Bytes

top-k-frequent-elements.md

File metadata and controls

35 lines (26 loc) · 731 Bytes

Top K Frequent Elements

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        
        unordered_map<int, int> cnt;
        
        auto cmp = [&](int a, int b) { return cnt[a] > cnt[b]; };
        priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
        
        for(int n : nums) cnt[n]++;
        
        for(auto &[n, c] : cnt) {
            pq.push(n);
        }
        
        vector<int> ans;
        
        while(pq.size() > k) {
            int c = pq.top();
            pq.pop();
        } 
        
        while(pq.size()) {
            int c = pq.top();
            pq.pop();
            ans.push_back(c);
        }
        
        return ans;
    }
};