-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path347. Top K Frequent Elements.java
40 lines (40 loc) · 1.35 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
class Solution {
private class FrequencyPair {
int frequency;
int data;
public FrequencyPair(int frequency, int data) {
this.frequency = frequency;
this.data = data;
}
}
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int elem : nums)
map.put(elem, map.getOrDefault(elem,0) + 1);
PriorityQueue<FrequencyPair> heap = new PriorityQueue<>((a, b) -> a.frequency - b.frequency);
for (int key : map.keySet()) {
heap.offer(createFrequencyPair(map, key));
if (heap.size() > k)
heap.poll();
}
int[] result = new int[k];
int counter = 0;
while (!heap.isEmpty()){
counter = pushDataIntoResult(heap, result, counter);
}
return result;
}
private int pushDataIntoResult(PriorityQueue<FrequencyPair> heap, int[] result, int counter) {
result[counter] = heap.poll().data;
counter++;
return counter;
}
private FrequencyPair createFrequencyPair(HashMap<Integer, Integer> map, int key) {
return new FrequencyPair(map.get(key), key);
}
}