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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| #include <iostream> #include <vector> #include <queue> #include <unordered_map> #include <algorithm> using namespace std;
void printArray(vector<int>& nums) { for (const int& num : nums) { cout << num << " "; } cout << endl; }
class Solution { public: vector<int> topKFrequent(vector<int>& nums, int k) { for (const int& num : nums) { num2cnt[num] ++; }
for (const auto& it : num2cnt) { heap.push({it.second, it.first}); if (heap.size() > k) { heap.pop(); } }
vector<int> res; res.reserve(k); while (heap.size()) { Node node = heap.top(); heap.pop(); res.push_back(node.num); }
return res; }
private: struct Node { int cnt; int num; bool operator<(const Node& t) const { if (cnt != t.cnt) return cnt > t.cnt; return num < t.num; } };
priority_queue<Node> heap; unordered_map<int, int> num2cnt; };
class Solution_1 { public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> num2cnt; for (const int& num : nums) { num2cnt[num] ++; }
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> heap(cmp); for (const auto& it : num2cnt) { int num = it.first, cnt = it.second; heap.push({num, cnt}); if (heap.size() > k) { heap.pop(); } }
vector<int> res; res.reserve(k); while (heap.size()) { res.emplace_back(heap.top().first); heap.pop(); }
return res; }
private: static bool cmp(pair<int, int>& a, pair<int, int>& b) { return a.second > b.second; } };
class Solution_2 { public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> num2cnt; for (const int& num : nums) { num2cnt[num] ++; }
auto comp = [](const pair<int, int>& a, const pair<int, int>& b) { return a.second > b.second || (a.second == b.second && a.first > b.first); }; priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> maxHeap(comp);
for (const auto& it : num2cnt) { int num = it.first, cnt = it.second; maxHeap.push({num, cnt}); if (maxHeap.size() > k) { maxHeap.pop(); } }
vector<int> res(k); int i = k - 1; while (maxHeap.size()) { res[i] = maxHeap.top().first; maxHeap.pop(); i --; }
return res; } };
class Solution_3 { public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> num2cnt; for (auto& v: nums) { num2cnt[v] ++; }
vector<pair<int, int>> values; for (auto& kv : num2cnt) { values.push_back(kv); }
vector<int> res; quickSort(values, 0, values.size() - 1, res, k); return res; } void quickSort(vector<pair<int, int>>& v, int start, int end, vector<int>& res, int k) { int picked = rand() % (end - start + 1) + start; swap(v[picked], v[start]);
int pivot = v[start].second; int index = start; for (int i = start + 1; i <= end; i ++) { if (v[i].second >= pivot) { swap(v[index + 1], v[i]); index ++; } } swap(v[start], v[index]);
if (k <= index - start) { quickSort(v, start, index - 1, res, k); } else { for (int i = start; i <= index; i++) { res.push_back(v[i].first); } if (k > index - start + 1) { quickSort(v, index + 1, end, res, k - (index - start + 1)); } } } };
int main() { Solution solution; vector<int> nums = {1, 1, 1, 2, 2, 3}; printArray(nums); int k = 2; vector<int> topK = solution.topKFrequent(nums, k); cout << "The " << k << " highest frequency elements are: "; for (int num : topK) { cout << num << " "; } cout << endl;
return 0; }
|