-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKFrequentElementsSolution.java
More file actions
39 lines (36 loc) · 1.32 KB
/
TopKFrequentElementsSolution.java
File metadata and controls
39 lines (36 loc) · 1.32 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
/**
* Given a non-empty array of integers, return the k most frequent elements.
*
* For example,
* Given [1,1,1,2,2,3] and k = 2, return [1,2].
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
* Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
*/
public class TopKFrequentElementsSolution {
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : nums) {
if (frequencyMap.containsKey(num)) {
frequencyMap.put(num, frequencyMap.get(num) + 1);
} else {
frequencyMap.put(num, 1);
}
}
List<Integer>[] bucket = new List[nums.length + 1];
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (bucket[entry.getValue()] == null) {
bucket[entry.getValue()] = new ArrayList<>();
}
bucket[entry.getValue()].add(entry.getKey());
}
List<Integer> elements = new ArrayList<>();
for (int i = bucket.length - 1; i > 0 && elements.size() < k; i--) {
if (bucket[i] != null) {
elements.addAll(bucket[i]);
}
}
return elements.subList(0, k);
}
}