-
Notifications
You must be signed in to change notification settings - Fork 0
/
0347. Top K Frequent Elements.cpp
76 lines (64 loc) · 2.42 KB
/
0347. 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution {
private:
vector<int> unique;
map<int, int> count_map;
public:
int partition(int left, int right, int pivot_index) {
int pivot_frequency = count_map[unique[pivot_index]];
// 1. move pivot to the end
swap(unique[pivot_index], unique[right]);
// 2. move all less frequent elements to the left
int store_index = left;
for (int i = left; i <= right; i++) {
if (count_map[unique[i]] < pivot_frequency) {
swap(unique[store_index], unique[i]);
store_index += 1;
}
}
// 3. move pivot to its final place
swap(unique[right], unique[store_index]);
return store_index;
}
void quickselect(int left, int right, int k_smallest) {
/*
Sort a list within left..right till kth less frequent element
takes its place.
*/
// base case: the list contains only one element
if (left == right) return;
int pivot_index = left + rand() % (right - left + 1);
// find the pivot position in a sorted list
pivot_index = partition(left, right, pivot_index);
// if the pivot is in its final sorted position
if (k_smallest == pivot_index) {
return;
} else if (k_smallest < pivot_index) {
// go left
quickselect(left, pivot_index - 1, k_smallest);
} else {
// go right
quickselect(pivot_index + 1, right, k_smallest);
}
}
vector<int> topKFrequent(vector<int>& nums, int k) {
// build hash map : element and how often it appears
for (int n : nums) {
count_map[n] += 1;
}
// array of unique elements
int n = count_map.size();
for (pair<int, int> p : count_map) {
unique.push_back(p.first);
}
// kth top frequent element is (n - k)th less frequent.
// Do a partial sort: from less frequent to the most frequent, till
// (n - k)th less frequent element takes its place (n - k) in a sorted array.
// All element on the left are less frequent.
// All the elements on the right are more frequent.
quickselect(0, n - 1, n - k);
// Return top k frequent elements
vector<int> top_k_frequent(k);
copy(unique.begin() + n - k, unique.end(), top_k_frequent.begin());
return top_k_frequent;
}
};