-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopKSolution.java
69 lines (59 loc) · 2.1 KB
/
TopKSolution.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
package com.milley.TopKFrequentElements;
import java.util.*;
public class TopKSolution {
public static List<Integer> topKFrequent(int[] nums, int k) {
List<Integer> elements = new ArrayList<>(k);
Map<Integer, Integer> map = new HashMap<>(k);
for (int i = 0; i < nums.length; i++) {
int key = nums[i];
if (map.get(key) != null) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, 1);
}
}
// Sort map by value
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
int index = 0;
for (int i = list.size() - 1; i >= 0; i--) {
if (index < k) {
elements.add(list.get(i).getKey());
}
index++;
}
return elements;
}
public static List<Integer> topKFrequentA(int[] nums, int k) {
HashMap<Integer, Integer> count = new HashMap<>();
for (int n : nums) {
count.put(n, count.getOrDefault(n, 0) + 1);
}
// init heap 'the less frequent element first'
PriorityQueue<Integer> heap = new PriorityQueue<>((n1, n2) -> count.get(n1) - count.get(n2));
// keep k top frequent elements in the heap
for (int n : count.keySet()) {
heap.add(n);
if (heap.size() > k) {
heap.poll();
}
}
// build output list
List<Integer> top_k = new LinkedList<>();
while (!heap.isEmpty()) {
top_k.add(heap.poll());
}
Collections.reverse(top_k);
return top_k;
}
public static void main(String[] args) {
//int[] nums = {1, 1, 1, 2, 2, 3};
int[] nums = {4, 1, -1, 2, -1, 2, 3};
System.out.println(topKFrequent(nums, 2));
}
}