Skip to content

Commit caa8a2c

Browse files
committed
update 347 java
1 parent 89076e0 commit caa8a2c

File tree

2 files changed

+956
-862
lines changed

2 files changed

+956
-862
lines changed

leetcode_java/src/main/java/LeetCodeJava/HashTable/TopKFrequentElements.java

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,92 @@
55
import java.util.*;
66
import java.util.stream.Collectors;
77

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+
*/
839
public class TopKFrequentElements {
940

1041
// V0
11-
// IDEA : HASHMAP + ARRAY ORDERING
42+
// IDEA : HASHMAP + CUSTOM ORDER PQ
1243
public int[] topKFrequent(int[] nums, int k) {
1344

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+
1494
if (nums.equals(null) || nums.length == 0){
1595
return null;
1696
}
@@ -54,9 +134,9 @@ public int[] topKFrequent(int[] nums, int k) {
54134
return res;
55135
}
56136

57-
// V0'
137+
// V0-2
58138
// IDEA : PQ (priority queue)
59-
public int[] topKFrequent_0(int[] nums, int k) {
139+
public int[] topKFrequent_0_2(int[] nums, int k) {
60140

61141
// O(1) time
62142
if (k == nums.length) {
@@ -120,9 +200,9 @@ public int[] topKFrequent_0(int[] nums, int k) {
120200
return top;
121201
}
122202

123-
// V0''
203+
// V0-3
124204
// 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) {
126206

127207
// Step 1. Count the frequency of each element
128208
Map<Integer, Integer> countMap = new HashMap<>();
@@ -153,9 +233,9 @@ public int[] topKFrequent_0_1(int[] nums, int k) {
153233
return topK;
154234
}
155235

156-
// V0'''
236+
// V0-4
157237
// IDEA : PQ + MAP
158-
public int[] topKFrequent_0_2(int[] nums, int k) {
238+
public int[] topKFrequent_0_4(int[] nums, int k) {
159239

160240
if (nums.length == 1){
161241
return nums;

0 commit comments

Comments
 (0)