Skip to content

Commit 1c1da53

Browse files
committed
Time: 12 ms (92.20%), Space: 13.8 MB (19.11%) - LeetHub
1 parent 03327ed commit 1c1da53

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
class cmp{
4+
public:
5+
bool operator()(const pair<int,int>& p1, const pair<int,int>& p2){
6+
return p1.first > p2.first;
7+
}
8+
};
9+
10+
vector<int> topKFrequent(vector<int>& nums, int k) {
11+
map<int,int> mp;
12+
for(auto x: nums)
13+
mp[x]++;
14+
15+
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;// max - heap
16+
17+
for(auto pp: mp){
18+
int x = pp.first;
19+
int y = pp.second;
20+
21+
cout << x << " " << y << endl;
22+
// pair<int,int> p = {y,x};
23+
pq.push({y,x});
24+
if(pq.size() > k)
25+
pq.pop();
26+
}
27+
28+
vector<int> req;
29+
while(!pq.empty()){
30+
req.push_back(pq.top().second);
31+
pq.pop();
32+
}
33+
34+
return req;
35+
}
36+
};
37+
//

0 commit comments

Comments
 (0)