-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path347.cpp
More file actions
26 lines (26 loc) · 692 Bytes
/
347.cpp
File metadata and controls
26 lines (26 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> count;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
for(auto& x: nums){
count[x]++;
}
for(auto& [x,cnt]:count){
if(pq.size()<k)
pq.push({cnt,x});
else{
if(cnt>pq.top().first){
pq.pop();
pq.push({cnt,x});
}
}
}
vector<int> res;
while(!pq.empty()){
res.push_back(pq.top().second);
pq.pop();
}
return res;
}
};