Skip to content

Instantly share code, notes, and snippets.

@apurvanandan1997
Created January 12, 2024 12:55
Show Gist options
  • Save apurvanandan1997/119ac0299a9d0069e8794e3166069bba to your computer and use it in GitHub Desktop.
Save apurvanandan1997/119ac0299a9d0069e8794e3166069bba to your computer and use it in GitHub Desktop.
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