-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
32 lines (30 loc) · 810 Bytes
/
program.cpp
File metadata and controls
32 lines (30 loc) · 810 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
#include "../include/pre.h"
#include <unordered_map>
vector<int> topKFrequent(vector<int>& nums, int k)
{
std::unordered_map<int, int> count;
for (auto n : nums)
{
if (count.find(n) != count.end()) count[n]++;
else count[n] = 1;
}
std::vector<std::pair<int, int>> vcount;
vcount.reserve(nums.size());
for (const auto& p : count)
{
vcount.emplace_back(p.second, p.first);
}
std::nth_element(vcount.begin(), vcount.begin() + k, vcount.end(), std::greater<typename decltype(vcount)::value_type>());
vector<int> rst;
for (auto it = vcount.begin(); it != vcount.begin() + k; it++)
{
rst.push_back(it->second);
}
return rst;
}
int main()
{
vector<int> t = {1,1,1,2,2,3};
cout << topKFrequent(t, 2);
return 0;
}