-
Notifications
You must be signed in to change notification settings - Fork 43
/
TopKFrequentElements.java
317 lines (268 loc) · 9.28 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package LeetCodeJava.HashTable;
// https://leetcode.com/problems/top-k-frequent-elements/
import java.util.*;
import java.util.stream.Collectors;
public class TopKFrequentElements {
// V0
// IDEA : HASHMAP + ARRAY ORDERING
public int[] topKFrequent(int[] nums, int k) {
if (nums.equals(null) || nums.length == 0){
return null;
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int x : nums){
if (!map.containsKey(x)){
map.put(x, 1);
}else{
int cur = map.get(x);
map.put(x, cur+1);
}
}
int _size = map.keySet().size();
// tmp array save (k, count) from map
// for ordering op below
int[][] tmp = new int[_size][2];
int z = 0;
for (int j : map.keySet()){
tmp[z][0] = j;
tmp[z][1] = map.get(j);
z += 1;
}
// order array
/** NOTE !!! sort syntax here
*
* -> Integer.compare(-x[1], -y[1]) for reverse ordering (descending)
*/
Arrays.sort(tmp, (x, y) -> Integer.compare(-x[1], -y[1]));
// System.out.println(">>> ");
// Arrays.stream(tmp).forEach(x -> System.out.println(x[0]));
// System.out.println(">>> ");
int[] res = new int[k];
for (int i = 0 ; i < k; i++){
res[i] = tmp[i][0];
}
return res;
}
// V0'
// IDEA : PQ (priority queue)
public int[] topKFrequent_0(int[] nums, int k) {
// O(1) time
if (k == nums.length) {
return nums;
}
// Step 1. build hash map : character and how often it appears
// O(N) time
Map<Integer, Integer> count = new HashMap();
for (int n: nums) {
count.put(n, count.getOrDefault(n, 0) + 1);
}
/** NOTE !!! how to init PQ below
*
*
* Priority Queue with Custom Comparator based on a Map (count):
*
* 1) This priority queue uses a custom comparator that compares the elements
* based on their values in a map (count).
*
* 2) count.get(n1) retrieves the frequency of n1 from the map,
* and count.get(n2) retrieves the frequency of n2.
*
* 3) The comparator orders the elements by their frequencies in ascending order.
* Elements with lower frequencies will
* have higher priority (i.e., they will be at the front of the queue).
*
*
* NOTE !!!
*
* Queue<Integer> heap = new PriorityQueue<>((n1, n2) -> count.get(n1) - count.get(n2));
*
* and
*
* PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
*
* are DIFFERENT
*
*/
// init heap 'the less frequent element first'
Queue<Integer> heap = new PriorityQueue<>((n1, n2) -> count.get(n1) - count.get(n2));
/** NOTE !!! here */
// Step 2. keep k top frequent elements in the heap
// O(N log k) < O(N log N) time
for (int n: count.keySet()) {
heap.add(n);
/** if size > k, remove smallest element (e.g. keep k top frequent elements in the heap) */
if (heap.size() > k){
heap.poll();
}
}
// Step 3. build an output array
// O(k log k) time
int[] top = new int[k];
for(int i = k - 1; i >= 0; --i) {
top[i] = heap.poll();
}
return top;
}
// V0''
// IDEA : HASH MAP + PQ (by GPT)
public int[] topKFrequent_0_1(int[] nums, int k) {
// Step 1. Count the frequency of each element
Map<Integer, Integer> countMap = new HashMap<>();
for (int num : nums) {
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
}
/** NOTE !!! how to init PQ below */
// Step 2. Use a Min-Heap (Priority Queue) to keep track of top K elements
PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>(
(a, b) -> a.getValue() - b.getValue()
);
// Step 3. Maintain the heap of size k
for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
heap.add(entry);
if (heap.size() > k) {
heap.poll(); // Remove the element with the smallest frequency
}
}
// Step 4. Extract the elements from the heap
int[] topK = new int[k];
for (int i = 0; i < k; i++) {
topK[i] = heap.poll().getKey();
}
return topK;
}
// V0'''
// IDEA : PQ + MAP
public int[] topKFrequent_0_2(int[] nums, int k) {
if (nums.length == 1){
return nums;
}
Map<Integer, Integer> map = new HashMap<>();
for (int x : nums){
Integer cnt = map.getOrDefault(x, 0);
map.put(x, cnt+1);
}
/** NOTE !!!
*
* init a PQ with "REVERSE order" with map value
*/
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> {
if(map.get(x) < map.get(y)){
return 1;
}else if (map.get(x) > map.get(y)){
return -1;
}
return 0;
});
for (int key : map.keySet()){
pq.add(key);
}
int[] res = new int[k];
for(int i = k - 1; i >= 0; --i) {
res[i] = pq.poll();
}
return res;
}
// V1
// IDEA : HEAP
// https://leetcode.com/problems/top-k-frequent-elements/editorial/
public int[] topKFrequent_2(int[] nums, int k) {
// O(1) time
if (k == nums.length) {
return nums;
}
// 1. build hash map : character and how often it appears
// O(N) time
Map<Integer, Integer> count = new HashMap();
for (int n: nums) {
count.put(n, count.getOrDefault(n, 0) + 1);
}
// init heap 'the less frequent element first'
Queue<Integer> heap = new PriorityQueue<>(
(n1, n2) -> count.get(n1) - count.get(n2));
// 2. keep k top frequent elements in the heap
// O(N log k) < O(N log N) time
for (int n: count.keySet()) {
heap.add(n);
if (heap.size() > k) heap.poll();
}
// 3. build an output array
// O(k log k) time
int[] top = new int[k];
for(int i = k - 1; i >= 0; --i) {
top[i] = heap.poll();
}
return top;
}
// V2
// IDEA : Quickselect (Hoare's selection algorithm)
// https://leetcode.com/problems/top-k-frequent-elements/editorial/
int[] unique;
Map<Integer, Integer> count;
public void swap(int a, int b) {
int tmp = unique[a];
unique[a] = unique[b];
unique[b] = tmp;
}
public int partition(int left, int right, int pivot_index) {
int pivot_frequency = count.get(unique[pivot_index]);
// 1. move pivot to end
swap(pivot_index, right);
int store_index = left;
// 2. move all less frequent elements to the left
for (int i = left; i <= right; i++) {
if (count.get(unique[i]) < pivot_frequency) {
swap(store_index, i);
store_index++;
}
}
// 3. move pivot to its final place
swap(store_index, right);
return store_index;
}
public void quickselect(int left, int right, int k_smallest) {
/*
Sort a list within left..right till kth less frequent element
takes its place.
*/
// base case: the list contains only one element
if (left == right) return;
// select a random pivot_index
Random random_num = new Random();
int pivot_index = left + random_num.nextInt(right - left);
// find the pivot position in a sorted list
pivot_index = partition(left, right, pivot_index);
// if the pivot is in its final sorted position
if (k_smallest == pivot_index) {
return;
} else if (k_smallest < pivot_index) {
// go left
quickselect(left, pivot_index - 1, k_smallest);
} else {
// go right
quickselect(pivot_index + 1, right, k_smallest);
}
}
public int[] topKFrequent_3(int[] nums, int k) {
// build hash map : character and how often it appears
count = new HashMap();
for (int num: nums) {
count.put(num, count.getOrDefault(num, 0) + 1);
}
// array of unique elements
int n = count.size();
unique = new int[n];
int i = 0;
for (int num: count.keySet()) {
unique[i] = num;
i++;
}
// kth top frequent element is (n - k)th less frequent.
// Do a partial sort: from less frequent to the most frequent, till
// (n - k)th less frequent element takes its place (n - k) in a sorted array.
// All element on the left are less frequent.
// All the elements on the right are more frequent.
quickselect(0, n - 1, n - k);
// Return top k frequent elements
return Arrays.copyOfRange(unique, n - k, n);
}
}