-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path347.cpp
More file actions
41 lines (34 loc) · 756 Bytes
/
Copy path347.cpp
File metadata and controls
41 lines (34 loc) · 756 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
map<int, int> t;
class Solution {
public:
struct cmp
{
bool operator()(int a, int b)
{
return t[a] >= t[b];
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
int len = nums.size();
t.clear();
for(int i = 0; i < len; ++i)
{
t[nums[i]]++;
}
priority_queue<int, vector<int>, cmp> p;
auto it = t.begin();
for(; it != t.end(); ++it)
{
cout << it->first << endl;
p.push(it->first);
if(p.size() > k)p.pop();
}
vector<int> ret;
while(p.size() > 0)
{
ret.push_back(p.top());
p.pop();
}
return ret;
}
};