forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
top-k-frequent-elements.cpp
51 lines (47 loc) · 1.35 KB
/
top-k-frequent-elements.cpp
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
42
43
44
45
46
47
48
49
50
51
// Time: O(n) ~ O(n^2), O(n) on average.
// Space: O(n)
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> counts;
for (const auto& i : nums) {
++counts[i];
}
vector<pair<int, int>> p;
for (auto it = counts.begin(); it != counts.end(); ++it) {
p.emplace_back(-(it->second), it->first);
}
nth_element(p.begin(), p.begin() + k - 1, p.end());
vector<int> result;
for (int i = 0; i < k; ++i) {
result.emplace_back(p[i].second);
}
return result;
}
};
// Time: O(nlogk)
// Space: O(n)
// Heap solution.
class Solution2 {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> counts;
for (int i = 0; i < nums.size(); ++i) {
++counts[nums[i]];
}
priority_queue<pair<int, int>> heap;
for (auto it = counts.begin(); it != counts.end(); ++it) {
heap.emplace(-(it->second), it->first);
if (heap.size() == k + 1) {
heap.pop();
}
}
vector<int> result;
while (!heap.empty()) {
result.emplace_back(heap.top().second);
heap.pop();
}
reverse(result.begin(), result.end());
return result;
}
};