-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTop_K_Frequent_Elements.cpp
More file actions
36 lines (33 loc) · 995 Bytes
/
Top_K_Frequent_Elements.cpp
File metadata and controls
36 lines (33 loc) · 995 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
#include "unordered_map"
#include "vector"
using namespace std;
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> freq;
int n = nums.size();
for (int i=0; i<n; i++) {
if (freq.find(nums[i]) == freq.end()) {
freq[nums[i]] = 1;
} else {
freq[nums[i]]++;
}
}
// at most, element can appear n times, so need array of n+1 size
vector<vector<int>> bucket(n+1);
for (auto i=freq.begin(); i!=freq.end(); i++) {
bucket[i->second].push_back(i->first);
}
vector<int> answer;
for (int i=n; i>=0; i--) {
if (answer.size() == k) {
break;
} else {
if (bucket[i].size() != 0) {
answer.insert(answer.end(), bucket[i].begin(), bucket[i].end());
}
}
}
return answer;
}
};