-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy path347.top-k-frequent-elements.java
47 lines (42 loc) · 1.13 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @lc app=leetcode id=347 lang=java
*
* [347] Top K Frequent Elements
*/
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return map.get(o1) - map.get(o2);
}
};
// 小顶堆 topk frequent elements
Queue<Integer> queue = new PriorityQueue<>(comparator);
for (int key : map.keySet()) {
if (queue.size() < k) {
queue.offer(key);
} else {
int value = queue.peek();
if (map.get(key) > map.get(value)) {
queue.poll();
queue.offer(key);
}
}
}
// 输出频率从高到低,每次从小顶堆弹出堆顶元素,插入列表最前面
List<Integer> result = new LinkedList<>();
while (!queue.isEmpty()) {
result.add(0, queue.poll());
}
return result;
}
}