blob: 343595fa96d6016e92ed6cd7e2c4231f2e03e8b9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include "leetcode.h"
class Solution {
public:
vector<int> topKFrequent(vector<int> &nums, int k) {
unordered_map<int, int> um;
for (int n : nums)
um[n]++;
vector<int> ans;
priority_queue<pair<int, int>> pqp;
for (auto it = um.begin(); it != um.end(); it++) {
pqp.push(make_pair(it->second, it->first));
if (pqp.size() > um.size() - k)
ans.push_back(pqp.top().second), pqp.pop();
}
return ans;
}
};
|