forked from fineanmol/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_k_frequent elements.cpp
More file actions
55 lines (38 loc) · 1.14 KB
/
top_k_frequent elements.cpp
File metadata and controls
55 lines (38 loc) · 1.14 KB
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
/*
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Example 3:
Input: nums = [1,2,1,2,1,2,3,1,3,2], k = 2
Output: [1,2]
*/
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
// Hash map to count frequencies of each number
unordered_map<int, int> freq;
// Count frequencies
for (int& num : nums)
freq[num]++; // Increment frequency count for each number
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
minHeap;
for (auto& pair : freq) {
int num = pair.first;
int count = pair.second;
minHeap.push({count, num});
if (minHeap.size() > k)
minHeap.pop();
}
vector<int> result;
while (!minHeap.empty()) {
result.push_back(minHeap.top().second);
minHeap.pop();
}
return result;
}
};