forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDKhan.cpp
More file actions
26 lines (21 loc) · 736 Bytes
/
Copy pathPDKhan.cpp
File metadata and controls
26 lines (21 loc) · 736 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> map;
vector<int> result;
for(int i = 0; i < nums.size(); i++){
map[nums[i]]++;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> minHeap;
for(auto i : map){
minHeap.push({i.second, i.first});
if(minHeap.size() > k)
minHeap.pop();
}
while(!minHeap.empty()){
result.push_back(minHeap.top().second);
minHeap.pop();
}
return result;
}
};