-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (32 loc) · 1.2 KB
/
Solution.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
class Solution {
class Frequency {
int value;
int frequency;
Frequency(int value, int frequency) {
this.value = value;
this.frequency = frequency;
}
}
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();
for (int num : nums) {
if (frequencyMap.get(num) == null)
frequencyMap.put(num, 1);
else
frequencyMap.put(num, frequencyMap.get(num) + 1);
}
Comparator<Frequency> frequencyComparator = new Comparator<Frequency>() {
public int compare(Frequency a, Frequency b) {
return a.frequency > b.frequency ? -1 : (a.frequency == b.frequency ? 0 : 1);
}
};
Queue<Frequency> queue = new PriorityQueue<Frequency>(frequencyComparator);
for (int key : frequencyMap.keySet()) {
queue.add(new Frequency(key, frequencyMap.get(key)));
}
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < k; i++)
res.add(queue.poll().value);
return res;
}
}