-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTopKFrequentElements.java
77 lines (75 loc) · 2.23 KB
/
TopKFrequentElements.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package io.ziheng.heap.leetcode;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
/**
* LeetCode 347. Top K Frequent Elements
* https://leetcode.com/problems/top-k-frequent-elements/
*/
public class TopKFrequentElements {
/**
* 主函数 -> 测试用例
*/
public static void main(String[] args) {
TopKFrequentElements obj = new TopKFrequentElements();
int[] nums = new int[]{-1, -1, };
int k = 1;
System.out.println(
obj.topKFrequent(nums, k)
);
nums = new int[]{1,1,1,2,2,3};
k = 2;
System.out.println(
obj.topKFrequent(nums, k)
);
}
private class Pair<L, R> {
public L left;
public R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
}
public List<Integer> topKFrequent(int[] nums, int k) {
if (nums == null || k < 0) {
return new LinkedList<>();
}
List<Integer> resultList = new LinkedList<>();
PriorityQueue<Pair<Integer, Integer>> priorityQueue =
new PriorityQueue<>(
new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(
Pair<Integer, Integer> o1,
Pair<Integer, Integer> o2) {
return o2.right - o1.right;
}
}
);
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (!map.containsKey(num)) {
map.put(num, 1);
} else {
map.put(num, 1 + map.get(num));
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
priorityQueue.offer(
new Pair<Integer, Integer>(entry.getKey(), entry.getValue())
);
}
for (int i = 0; i < k; i++) {
Pair<Integer, Integer> pair = priorityQueue.poll();
resultList.add(
pair.left
);
}
return resultList;
}
}
/* EOF */