forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_347.java
108 lines (100 loc) · 3.8 KB
/
_347.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.TreeMap;
public class _347 {
public static class Solution1 {
/**
* Use buckets to hold numbers of the same frequency
* It's averaged at 30 ms on Leetcode.
*/
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap();
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
ArrayList[] bucket = new ArrayList[nums.length + 1];
for (Entry<Integer, Integer> e : map.entrySet()) {
int frequency = e.getValue();
if (bucket[frequency] == null) {
bucket[frequency] = new ArrayList<Integer>();
}
bucket[frequency].add(e.getKey());
}
List<Integer> result = new ArrayList<>();
for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) {
if (bucket[i] != null) {
for (int j = 0; j < bucket[i].size(); j++) {
result.add((int) bucket[i].get(j));
}
}
}
int[] arr = new int[result.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = result.get(i);
}
return arr;
}
}
public static class Solution2 {
/**
* Use hashtable and heap, it's averaged at 100 ms on Leetocde.
*/
public int[] topKFrequent(int[] nums, int k) {
// construct the frequency map first, and then iterate through the map
// and put them into the heap, this is O(n)
Map<Integer, Integer> map = new HashMap();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// build heap, this is O(logn)
Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
for (Entry<Integer, Integer> entry : map.entrySet()) {
heap.offer(entry);
}
List<Integer> result = new ArrayList();
while (k-- > 0) {
result.add(heap.poll().getKey());
}
int[] arr = new int[result.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = result.get(i);
}
return arr;
}
}
public static class Solution3 {
/**
* Use hashtable and heap, it's averaged at 10 ms on Leetocde.
*/
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
TreeMap<Integer, List<Integer>> treeMap = new TreeMap<>((a, b) -> b - a);
for (int key : map.keySet()) {
List<Integer> list = treeMap.getOrDefault(map.get(key), new ArrayList<>());
list.add(key);
treeMap.put(map.get(key), list);
}
List<Integer> list = new ArrayList<>();
while (!treeMap.isEmpty()) {
list.addAll(treeMap.pollFirstEntry().getValue());
if (list.size() == k) {
break;
}
}
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
}
}