-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrequentElementsQuickSelect.cpp
93 lines (77 loc) · 2.47 KB
/
FrequentElementsQuickSelect.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Top K frequent elements
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/799/
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cstdlib>
void print(const std::vector<int>& numbers)
{
if (numbers.empty()) {
return;
}
for (auto num : numbers) {
std::cout << num << " ";
}
std::cout << "\n";
}
int partition(const int left, const int right, const int kSmallest,
std::vector<int>& uniqueKeys, std::unordered_map<int, int>& freqMap)
{
int pivotFrequency = freqMap[uniqueKeys[kSmallest]];
std::swap(uniqueKeys[kSmallest], uniqueKeys[right]);
int storeIndex = left;
for (int i = left; i <= right; ++i) {
if (freqMap[uniqueKeys[i]] < pivotFrequency) {
std::swap(uniqueKeys[i], uniqueKeys[storeIndex]);
storeIndex += 1;
}
}
std::swap(uniqueKeys[right], uniqueKeys[storeIndex]);
return storeIndex;
}
void quickselect(const int left, const int right, const int kSmallest,
std::vector<int>& uniqueKeys, std::unordered_map<int, int>& freqMap)
{
if (left == right) {
return;
}
int pivotIndex = left + std::rand() % (right - left + 1);
pivotIndex = partition(left, right, pivotIndex, uniqueKeys, freqMap);
if (pivotIndex == kSmallest) {
return;
} else if (kSmallest < pivotIndex) {
quickselect(left, pivotIndex - 1, kSmallest, uniqueKeys, freqMap);
} else {
quickselect(pivotIndex + 1, right, kSmallest, uniqueKeys, freqMap);
}
}
std::vector<int> topKFrequent(std::vector<int>& numbers, const int k)
{
std::unordered_map<int, int> freqMap;
for (auto num : numbers) {
freqMap[num]++;
}
size_t n = freqMap.size();
std::vector<int> uniqueKeys {};
for (auto p : freqMap) {
uniqueKeys.push_back(p.first);
}
quickselect(0, n - 1, n - k, uniqueKeys, freqMap);
std::vector<int> kFrequent(k);
std::copy(uniqueKeys.begin() + n - k, uniqueKeys.end(), kFrequent.begin());
return kFrequent;
}
int main()
{
std::vector<int> numbers {3, 1, 4, 4, 5, 2, 6, 1};
std::cout << "Numbers : ";
print(numbers);
std::cout << "---------------------------------------\n";
int k = 2;
std::cout << "Top "<< k << " frequent numbers : \n";
std::vector<int> frequent = topKFrequent(numbers, k);
print(frequent);
std::cout << "=======================================\n";
return 0;
}