-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lc0347Solution.java
34 lines (31 loc) · 994 Bytes
/
Lc0347Solution.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
32
33
34
package top;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class Lc0347Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
PriorityQueue<int[]> queue = new PriorityQueue<>((m, n) -> {
return m[1] - n[1];
});
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
int num = e.getKey(), count = e.getValue();
if (queue.size() == k) {
if (queue.peek()[1] < count) {
queue.poll();
queue.offer(new int[] {num, count});
}
} else {
queue.offer(new int[] {num, count});
}
}
int[] ret = new int[k];
for (int i = 0; i < k; i++) {
ret[i] = queue.poll()[0];
}
return ret;
}
}