|
5 | 5 | import java.util.*; |
6 | 6 | import java.util.stream.Collectors; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * 347. Top K Frequent Elements |
| 10 | + * Solved |
| 11 | + * Medium |
| 12 | + * Topics |
| 13 | + * Companies |
| 14 | + * Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. |
| 15 | + * |
| 16 | + * |
| 17 | + * |
| 18 | + * Example 1: |
| 19 | + * |
| 20 | + * Input: nums = [1,1,1,2,2,3], k = 2 |
| 21 | + * Output: [1,2] |
| 22 | + * Example 2: |
| 23 | + * |
| 24 | + * Input: nums = [1], k = 1 |
| 25 | + * Output: [1] |
| 26 | + * |
| 27 | + * |
| 28 | + * Constraints: |
| 29 | + * |
| 30 | + * 1 <= nums.length <= 105 |
| 31 | + * -104 <= nums[i] <= 104 |
| 32 | + * k is in the range [1, the number of unique elements in the array]. |
| 33 | + * It is guaranteed that the answer is unique. |
| 34 | + * |
| 35 | + * |
| 36 | + * Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size. |
| 37 | + * |
| 38 | + */ |
8 | 39 | public class TopKFrequentElements { |
9 | 40 |
|
10 | 41 | // V0 |
11 | | - // IDEA : HASHMAP + ARRAY ORDERING |
| 42 | + // IDEA : HASHMAP + CUSTOM ORDER PQ |
12 | 43 | public int[] topKFrequent(int[] nums, int k) { |
13 | 44 |
|
| 45 | + Map<Integer, Integer> map = new HashMap<>(); |
| 46 | + for (Integer x : nums) { |
| 47 | + map.put(x, map.getOrDefault(x, 0) + 1); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * NOTE !!! |
| 52 | + * |
| 53 | + * we set PQ as Integer type, (PriorityQueue<Integer>) |
| 54 | + * and order PQ by map value (descending order) (x, y) -> map.get(x) - map.get(y) |
| 55 | + * |
| 56 | + * |
| 57 | + * |
| 58 | + * below is wrong (no need to put Map.Entry into PQ) |
| 59 | + * |
| 60 | + * // PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>( |
| 61 | + * // (x, y) -> map.get(x) - map.get(y) |
| 62 | + * // ); |
| 63 | + * |
| 64 | + */ |
| 65 | + // PQ with custom logic |
| 66 | + PriorityQueue<Integer> pq = new PriorityQueue<>( |
| 67 | + (x, y) -> map.get(x) - map.get(y) |
| 68 | + ); |
| 69 | + |
| 70 | + // NOTE !!! add map element to PQ |
| 71 | + for (Integer key : map.keySet()){ |
| 72 | + pq.add(key); |
| 73 | + // pop element is size > k |
| 74 | + if (pq.size() > k){ |
| 75 | + pq.poll(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // pop elements from PQ |
| 80 | + int tmp = 0; |
| 81 | + int[] res = new int[k]; |
| 82 | + while (tmp < k) { |
| 83 | + res[tmp] = pq.poll(); |
| 84 | + tmp += 1; |
| 85 | + } |
| 86 | + |
| 87 | + return res; |
| 88 | + } |
| 89 | + |
| 90 | + // V0-1 |
| 91 | + // IDEA : HASHMAP + ARRAY ORDERING |
| 92 | + public int[] topKFrequent_0_1(int[] nums, int k) { |
| 93 | + |
14 | 94 | if (nums.equals(null) || nums.length == 0){ |
15 | 95 | return null; |
16 | 96 | } |
@@ -54,9 +134,9 @@ public int[] topKFrequent(int[] nums, int k) { |
54 | 134 | return res; |
55 | 135 | } |
56 | 136 |
|
57 | | - // V0' |
| 137 | + // V0-2 |
58 | 138 | // IDEA : PQ (priority queue) |
59 | | - public int[] topKFrequent_0(int[] nums, int k) { |
| 139 | + public int[] topKFrequent_0_2(int[] nums, int k) { |
60 | 140 |
|
61 | 141 | // O(1) time |
62 | 142 | if (k == nums.length) { |
@@ -120,9 +200,9 @@ public int[] topKFrequent_0(int[] nums, int k) { |
120 | 200 | return top; |
121 | 201 | } |
122 | 202 |
|
123 | | - // V0'' |
| 203 | + // V0-3 |
124 | 204 | // IDEA : HASH MAP + PQ (by GPT) |
125 | | - public int[] topKFrequent_0_1(int[] nums, int k) { |
| 205 | + public int[] topKFrequent_0_3(int[] nums, int k) { |
126 | 206 |
|
127 | 207 | // Step 1. Count the frequency of each element |
128 | 208 | Map<Integer, Integer> countMap = new HashMap<>(); |
@@ -153,9 +233,9 @@ public int[] topKFrequent_0_1(int[] nums, int k) { |
153 | 233 | return topK; |
154 | 234 | } |
155 | 235 |
|
156 | | - // V0''' |
| 236 | + // V0-4 |
157 | 237 | // IDEA : PQ + MAP |
158 | | - public int[] topKFrequent_0_2(int[] nums, int k) { |
| 238 | + public int[] topKFrequent_0_4(int[] nums, int k) { |
159 | 239 |
|
160 | 240 | if (nums.length == 1){ |
161 | 241 | return nums; |
|
0 commit comments