LeetCode 347 - Top K Frequent Elements
LeetCode 347 - Top K Frequent Elements
Table of Contents
Problem Statement
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Using Bucket Sort
We use Bucket Sort to distribute the elements of an array into a number of buckets.
Counting Frequencies:
- The hash map
freqassociates each element innumswith its count.
- The hash map
Bucket Sort:
Elements with the same frequency are grouped into buckets (e.g., all elements appearing 3 times go into bucket 3).
The maximum frequency cannot exceed the size of the array
n.
Retrieve Top
kElements:Start from the bucket with the highest frequency and work downward.
Collect elements until
kelements are retrieved.
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
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> freq;
for (int num : nums) {
freq[num]++;
}
int n = nums.size();
vector<vector<int>> buckets(n + 1);
for (auto& pair : freq) {
int element = pair.first;
int count = pair.second;
buckets[count].push_back(element);
}
vector<int> result;
for (int i = n; i >= 0 && result.size() < k; --i) {
for (int element : buckets[i]) {
result.push_back(element);
if (result.size() == k) break;
}
}
return result;
}
};
Complexity
- Time Complexity:
- Counting frequencies: 𝑂(𝑛), where 𝑛 is the size of nums.
- Bucket traversal: 𝑂(𝑛) (each element is processed once).
- Total: 𝑂(𝑛).
- Space Complexity:
- 𝑂(𝑛) for the buckets and frequency map.
Conclusion
This implementation efficiently returns the k most frequent elements, in any order.