forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKFrequent.java
More file actions
44 lines (40 loc) · 1.2 KB
/
TopKFrequent.java
File metadata and controls
44 lines (40 loc) · 1.2 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
package map;
import java.util.*;
public class TopKFrequent {
public static void main(String[] args) {
// int[] nums = {1,1,1,2,2,3};
int[] nums = {4,1,-1,2,-1,2,3};
int k = 2;
int[] r = topKFrequent(nums,k);
System.out.println(Arrays.toString(r));
}
private static int[] 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);
}
}
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return map.get(a) - map.get(b);
}
});
for (Integer key : map.keySet()){
if (pq.size() < k){
pq.add(key);
}else if (map.get(key) > map.get(pq.peek())){
pq.remove();
pq.add(key);
}
}
int[] res = new int[k];
for (int i = 0; i < k; i++) {
res[i] = pq.remove();
}
return res;
}
}