-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path347.top-k-frequent-elements.java
41 lines (39 loc) · 1.07 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
/*
* @lc app=leetcode id=347 lang=java
*
* [347] Top K Frequent Elements
*/
// @lc code=start
class Solution {
/*
freq有上限,better than O(logn)可以使用bucket sort
time: O(n)
space: O(n)
*/
public int[] topKFrequent(int[] nums, int k) {
int n = nums.length;
List[] bucket = new List[n + 1];
Map<Integer, Integer> count = new HashMap<>();
for (int num : nums) {
count.put(num, count.getOrDefault(num, 0) + 1);
}
for (int key : count.keySet()) {
int freq = count.get(key);
if (bucket[freq] == null) {
bucket[freq] = new ArrayList<Integer>();
}
bucket[freq].add(key);
}
int[] res = new int[k];
int pos = 0;
for (int i = n; pos < k && i > 0; --i) {
if (bucket[i] != null) {
for (int j = 0; j < bucket[i].size() && pos < k; ++j) {
res[pos++] = (int)bucket[i].get(j);
}
}
}
return res;
}
}
// @lc code=end