-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTopKFrequent.java
More file actions
51 lines (48 loc) · 1.45 KB
/
TopKFrequent.java
File metadata and controls
51 lines (48 loc) · 1.45 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
45
46
47
48
49
50
51
package com.algorithm.stacksandqueues;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class TopKFrequent {
/**
* #347
* https://leetcode-cn.com/problems/top-k-frequent-elements/
* @param args
*/
public static void main(String[] args) {
System.out.println(Arrays.toString(topKFrequent(new int[]{2, 2, 1, 1, 1, 3}, 2)));
System.out.println(Arrays.toString(topKFrequent(new int[]{4,1,-1,2,-1,2,3}, 2)));
}
public static int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
PriorityQueue<Pair> queue = new PriorityQueue<>(k, Comparator.comparingInt(a -> a.v));
for (Map.Entry<Integer, Integer> m : map.entrySet()) {
if (queue.size() < k) {
queue.offer(new Pair(m.getKey(), m.getValue()));
continue;
}
if (m.getValue() > queue.peek().v) {
queue.poll();
queue.offer(new Pair(m.getKey(), m.getValue()));
}
}
int[] res = new int[k];
int i = 0;
while (i < k) {
res[i++] = queue.poll().k;
}
return res;
}
}
class Pair {
int k;
int v;
public Pair(int k, int v) {
this.k = k;
this.v = v;
}
}