Created
January 12, 2024 12:55
-
-
Save apurvanandan1997/119ac0299a9d0069e8794e3166069bba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> topKFrequent(vector<int>& nums, int k) { | |
vector<int> ret; | |
priority_queue <unsigned long long> q; | |
unordered_map <int, int> h; | |
for (int i = 0; i< nums.size(); i++) { | |
h[nums[i]] = h[nums[i]]+1; | |
} | |
for(auto it = h.begin(); it!=h.end(); it++) { | |
q.push((it->first & 0xffffffff) | ((unsigned long long)it->second << 32)); | |
} | |
for(int i = 0; i<k; i++) { | |
int x = q.top() & 0xffffffff; | |
q.pop(); | |
ret.push_back(x); | |
} | |
return ret; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment