-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopKFrequentElements.java
48 lines (40 loc) · 1.22 KB
/
TopKFrequentElements.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
package array;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 08/27/2019
*
* 347. Top K Frequent Elements
* https://leetcode.com/problems/top-k-frequent-elements/
* Difficulty: Medium
*
* Approach: Map & Reverse
* Runtime: 13 ms, faster than 70.89% of Java online submissions for Top K Frequent Elements.
* Memory Usage: 40.8 MB, less than 56.03% of Java online submissions for Top K Frequent Elements.
*
* @see ArrayTest#testTopKFrequentElements()
*/
public class TopKFrequentElements {
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<Integer, Integer>();
Map<Integer, Set<Integer>> reverse = new TreeMap<Integer, Set<Integer>>(Collections.reverseOrder());
List<Integer> list = new LinkedList<Integer>();
for (int n : nums) {
count.put(n, count.getOrDefault(n, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
if (!reverse.containsKey(entry.getValue())) {
reverse.put(entry.getValue(), new HashSet<Integer>());
}
reverse.get(entry.getValue()).add(entry.getKey());
}
for (Set<Integer> set : reverse.values()) {
list.addAll(set);
k -= set.size();
if (k == 0) {
break;
}
}
return list;
}
}