-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKthFrequentElements.java
More file actions
47 lines (37 loc) · 1.4 KB
/
TopKthFrequentElements.java
File metadata and controls
47 lines (37 loc) · 1.4 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
package solutions;
import java.util.*;
// [Problem] https://leetcode.com/problems/top-k-frequent-elements/
class TopKthFrequentElements {
// test
public static void main(String[] args) {
TopKthFrequentElements solution = new TopKthFrequentElements();
int[] expectedOutput = {1, 2};
int[] actualOutput = solution.topKFrequent(new int[]{1, 1, 1, 2, 2, 3}, 2);
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
}
// Heap solution - O(NlogK) time, O(N) space
public int[] topKFrequent(int[] nums, int k) {
if (k == nums.length) {
return nums;
}
// 1. Build counts map - O(N) time
Map<Integer, Integer> counts = new HashMap<>();
for (int num : nums) {
counts.put(num, counts.getOrDefault(num, 0) + 1);
}
// 2. Keep k top frequent elements in the heap - O(NlogK) time
Queue<Integer> heap = new PriorityQueue<>((num1, num2) -> counts.get(num1) - counts.get(num2));
for (int num : counts.keySet()) {
heap.add(num);
if (heap.size() > k) {
heap.poll();
}
}
// 3. Build an output array - O(KlogK) time
int[] topFrequentNums = new int[k];
for (int i = k - 1; i >= 0; i--) {
topFrequentNums[i] = heap.poll();
}
return topFrequentNums;
}
}