-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy path75--top-k-frequent-elements.java
More file actions
28 lines (28 loc) · 1.03 KB
/
Copy path75--top-k-frequent-elements.java
File metadata and controls
28 lines (28 loc) · 1.03 KB
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
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> freqMap = new HashMap<>(); // O(n)
for (int num : nums) { // O(n)
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
ArrayList<Integer>[] bucket = new ArrayList[nums.length + 1]; // O(n)
for (Integer key : freqMap.keySet()) { // O(n)
int frequency = freqMap.get(key);
if (bucket[frequency] == null) {
bucket[frequency] = new ArrayList<>();
}
bucket[frequency].add(key);
}
List<Integer> list = new ArrayList<>(); // O(n)
for (int i = bucket.length - 1; i >= 0; i--) { // O(2n) ~ O(n)
if (bucket[i] != null) {
list.addAll(bucket[i]);
}
if (list.size() == k) break;
}
int[] ans = new int[list.size()]; // O(n)
for (int i = 0; i < ans.length; i++) { // O(n)
ans[i] = list.get(i);
}
return ans;
}
} // TC: O(n), SC: O(n)