Skip to content

Commit 2c63bc1

Browse files
committed
test complete topKFrequent problem
1 parent 55db6e9 commit 2c63bc1

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

contains-duplicate/Geegong.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,73 @@ public class Geegong {
88
public int[] topKFrequent(int[] nums, int k) {
99
int[] result = new int[k];
1010

11-
// key : key / value : count
12-
Map<Integer, Integer> keyMap = new HashMap<>();
11+
// key : num element in nums / value : frequency of num elements
12+
Map<Integer, Integer> numMap = new HashMap<>();
1313

14-
// key : count / value : HashSet<Integer> keys
15-
Map<Integer, HashSet<Integer>> countMap = new HashMap<>();
14+
// key : frequency of num elements / value : HashSet<Integer> num elements
15+
Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>();
1616

17-
// 가장 많이 출연한 친구
17+
// most frequent numbers
1818
int maxCount = 0;
1919

20+
// initialize numMap
2021
for (int num : nums) {
21-
int countToBePut = 0;
22+
if (numMap.containsKey(num)) {
23+
Integer alreadyCounted = numMap.get(num);
24+
numMap.put(num, alreadyCounted + 1);
25+
} else {
26+
numMap.put(num, 1);
27+
}
28+
}
29+
30+
31+
//numHashSetMap
32+
for (int num : numMap.keySet()) {
33+
int frequencyOfNum = numMap.get(num);
34+
maxCount = Math.max(maxCount, frequencyOfNum);
2235

23-
if (keyMap.containsKey(num)) {
24-
Integer alreadyCounted = keyMap.get(num);
25-
countToBePut = alreadyCounted + 1;
26-
keyMap.put(num, countToBePut);
36+
if (frequencyMap.containsKey(frequencyOfNum)) {
37+
HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum);
38+
alreadySet.add(num);
39+
40+
frequencyMap.put(frequencyOfNum, alreadySet);
2741

28-
checkCountMapAndPutCount(countMap, num, countToBePut);
2942
} else {
30-
countToBePut = 1;
31-
keyMap.put(num, countToBePut);
32-
checkCountMapAndPutCount(countMap, num, countToBePut);
33-
}
43+
HashSet<Integer> newHashSet = new HashSet<>();
44+
newHashSet.add(num);
3445

35-
maxCount = Math.max(maxCount, countToBePut);
46+
frequencyMap.put(frequencyOfNum, newHashSet);
47+
}
3648
}
3749

3850

39-
// 가장 큰 숫자부터 꺼내볼까요오오오 우후
40-
int kIndex=0;
41-
for (int index=maxCount; index >= 0; index--) {
42-
if (kIndex >= k) {
51+
// maxCount 부터 decreasing
52+
int resultIndex=0;
53+
for(int frequency=maxCount; frequency>=0; frequency--) {
54+
if (resultIndex >= result.length) {
4355
return result;
4456
}
4557

46-
if (countMap.containsKey(index)) {
47-
HashSet<Integer> hashNums = countMap.get(index);
48-
Iterator<Integer> iterator = hashNums.iterator();
49-
while(iterator.hasNext()) {
58+
if (frequencyMap.containsKey(frequency)) {
59+
HashSet<Integer> numElements = frequencyMap.get(frequency);
60+
61+
for (int numElement : numElements) {
62+
result[resultIndex] = numElement;
63+
resultIndex++;
5064

51-
if (kIndex >= k) {
65+
66+
if (resultIndex >= result.length) {
5267
return result;
5368
}
54-
55-
result[kIndex] = iterator.next();
56-
kIndex++;
5769
}
58-
5970
}
60-
6171
}
6272

6373
return result;
64-
65-
6674
}
6775

68-
public void checkCountMapAndPutCount(Map<Integer, HashSet<Integer>> countMap, int key, int count) {
69-
if (countMap.containsKey(count)) {
70-
HashSet<Integer> already = countMap.get(count);
71-
already.add(key);
72-
} else {
73-
HashSet<Integer> newSet = new HashSet<>();
74-
newSet.add(key);
75-
countMap.put(count, newSet);
76-
}
77-
76+
public boolean checkIsFull(int[] arr, int currIndex) {
77+
return currIndex >= arr.length;
7878
}
7979

8080
}

0 commit comments

Comments
 (0)