-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path347-Top-K-Frequent-Elements.java
31 lines (27 loc) · 1.12 KB
/
347-Top-K-Frequent-Elements.java
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
class Solution {
public int[] topKFrequent(int[] nums, int k) {
// Step 1: Count frequency of each number
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : nums) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
// Step 2: Use a min-heap to find the top k frequent elements
PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(
(a, b) -> a.getValue() - b.getValue() // Min-heap based on frequency
);
// Add all elements to the heap, but keep the size of heap <= k
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
minHeap.offer(entry);
if (minHeap.size() > k) {
minHeap.poll(); // Remove the element with the smallest frequency
}
}
// Step 3: Extract the elements from the heap
int[] result = new int[k];
int index = 0;
while (!minHeap.isEmpty()) {
result[index++] = (minHeap.poll().getKey());
}
return result;
}
}