|
1 | | -import java.util.HashMap; |
2 | | -import java.util.HashSet; |
3 | | -import java.util.Map; |
| 1 | +import java.util.*; |
4 | 2 |
|
5 | | -public class Geegong { |
6 | 3 |
|
7 | 4 |
|
8 | | - public int[] topKFrequent(int[] nums, int k) { |
9 | | - int[] result = new int[k]; |
| 5 | +public class Geegong { |
10 | 6 |
|
11 | | - // key : num element in nums / value : frequency of num elements |
12 | | - Map<Integer, Integer> numMap = new HashMap<>(); |
13 | 7 |
|
14 | | - // key : frequency of num elements / value : HashSet<Integer> num elements |
15 | | - Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>(); |
| 8 | + /** |
| 9 | + * Map 으로 빈도수를 key , 빈도수에 해당되는 num 들을 list 로 저장 |
| 10 | + * key (빈도수) 를 sorting |
| 11 | + * k 만큼 골라낸다 |
| 12 | + * Time Complexity : O(N) + O(N logN) + O(N) |
| 13 | + * - O(N) : nums 만큼 iterate |
| 14 | + * - O(N log N) : sorting |
| 15 | + * - O(N) : frequency 그룹핑된 그룹 갯수만큼 iterate |
| 16 | + * @param nums |
| 17 | + * @param k |
| 18 | + * @return int[] |
| 19 | + */ |
| 20 | + public int[] topKFrequent(int[] nums, int k) { |
16 | 21 |
|
17 | | - // most frequent numbers |
18 | | - int maxCount = 0; |
| 22 | + // key : frequency , value : list of nums |
| 23 | + Map<Integer, List<Integer>> map = new HashMap<>(); |
| 24 | + Arrays.sort(nums); |
| 25 | + int current = nums[0]; |
| 26 | + int count = 1; |
19 | 27 |
|
20 | | - // initialize numMap |
21 | | - for (int num : nums) { |
22 | | - if (numMap.containsKey(num)) { |
23 | | - Integer alreadyCounted = numMap.get(num); |
24 | | - numMap.put(num, alreadyCounted + 1); |
| 28 | + for (int i = 1; i < nums.length; i++) { |
| 29 | + if (nums[i] == current) { |
| 30 | + count++; |
25 | 31 | } else { |
26 | | - numMap.put(num, 1); |
| 32 | + map.computeIfAbsent(count, el -> new ArrayList<>()).add(current); |
| 33 | + current = nums[i]; |
| 34 | + count = 1; |
27 | 35 | } |
28 | 36 | } |
29 | 37 |
|
| 38 | + // Add last group |
| 39 | + map.computeIfAbsent(count, el -> new ArrayList<>()).add(current); |
30 | 40 |
|
31 | | - //numHashSetMap |
32 | | - for (int num : numMap.keySet()) { |
33 | | - int frequencyOfNum = numMap.get(num); |
34 | | - maxCount = Math.max(maxCount, frequencyOfNum); |
35 | | - |
36 | | - if (frequencyMap.containsKey(frequencyOfNum)) { |
37 | | - HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum); |
38 | | - alreadySet.add(num); |
39 | | - |
40 | | - frequencyMap.put(frequencyOfNum, alreadySet); |
41 | | - |
42 | | - } else { |
43 | | - HashSet<Integer> newHashSet = new HashSet<>(); |
44 | | - newHashSet.add(num); |
45 | | - |
46 | | - frequencyMap.put(frequencyOfNum, newHashSet); |
47 | | - } |
48 | | - } |
| 41 | + List<Integer> sortedFrequency = map.keySet().stream().sorted(Comparator.reverseOrder()).toList(); |
| 42 | + List<Integer> result = new ArrayList<>(); |
| 43 | + for (int index=0; index<sortedFrequency.size(); index++ ) { |
| 44 | + int mostFrequency = sortedFrequency.get(index); |
| 45 | + List<Integer> numsOfFreq = map.get(mostFrequency); |
49 | 46 |
|
| 47 | + for(int innerIndex = 0; innerIndex<numsOfFreq.size(); innerIndex++) { |
| 48 | + if (result.size() == k) { |
| 49 | + return result.stream().mapToInt(Integer::intValue).toArray(); |
| 50 | + } |
50 | 51 |
|
51 | | - // maxCount 부터 decreasing |
52 | | - int resultIndex=0; |
53 | | - for(int frequency=maxCount; frequency>=0; frequency--) { |
54 | | - if (resultIndex >= result.length) { |
55 | | - return result; |
| 52 | + result.add(numsOfFreq.get(innerIndex)); |
56 | 53 | } |
57 | 54 |
|
58 | | - if (frequencyMap.containsKey(frequency)) { |
59 | | - HashSet<Integer> numElements = frequencyMap.get(frequency); |
60 | | - |
61 | | - for (int numElement : numElements) { |
62 | | - result[resultIndex] = numElement; |
63 | | - resultIndex++; |
64 | | - |
65 | | - |
66 | | - if (resultIndex >= result.length) { |
67 | | - return result; |
68 | | - } |
69 | | - } |
70 | | - } |
71 | 55 | } |
72 | 56 |
|
73 | | - return result; |
| 57 | + return result.stream().mapToInt(Integer::intValue).toArray(); |
| 58 | + |
| 59 | +// 아래 문제풀이는 예전 기수에 풀었던 방법으로 Map 으로 빈도수와 num을 관리하는 값을 가지긴 하나 |
| 60 | +// sorting은 하지 않고 maxNumOfFrequency를 구하여 순차적으로 작은 값들을 꺼내서 k만큼 리턴한다 |
| 61 | +// int[] result = new int[k]; |
| 62 | +// |
| 63 | +// // key : num element in nums / value : frequency of num elements |
| 64 | +// Map<Integer, Integer> numMap = new HashMap<>(); |
| 65 | +// |
| 66 | +// // key : frequency of num elements / value : HashSet<Integer> num elements |
| 67 | +// Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>(); |
| 68 | +// |
| 69 | +// // most frequent numbers |
| 70 | +// int maxCount = 0; |
| 71 | +// |
| 72 | +// // initialize numMap |
| 73 | +// for (int num : nums) { |
| 74 | +// if (numMap.containsKey(num)) { |
| 75 | +// Integer alreadyCounted = numMap.get(num); |
| 76 | +// numMap.put(num, alreadyCounted + 1); |
| 77 | +// } else { |
| 78 | +// numMap.put(num, 1); |
| 79 | +// } |
| 80 | +// } |
| 81 | +// |
| 82 | +// |
| 83 | +// //numHashSetMap |
| 84 | +// for (int num : numMap.keySet()) { |
| 85 | +// int frequencyOfNum = numMap.get(num); |
| 86 | +// maxCount = Math.max(maxCount, frequencyOfNum); |
| 87 | +// |
| 88 | +// if (frequencyMap.containsKey(frequencyOfNum)) { |
| 89 | +// HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum); |
| 90 | +// alreadySet.add(num); |
| 91 | +// |
| 92 | +// frequencyMap.put(frequencyOfNum, alreadySet); |
| 93 | +// |
| 94 | +// } else { |
| 95 | +// HashSet<Integer> newHashSet = new HashSet<>(); |
| 96 | +// newHashSet.add(num); |
| 97 | +// |
| 98 | +// frequencyMap.put(frequencyOfNum, newHashSet); |
| 99 | +// } |
| 100 | +// } |
| 101 | +// |
| 102 | +// |
| 103 | +// // maxCount 부터 decreasing |
| 104 | +// int resultIndex=0; |
| 105 | +// for(int frequency=maxCount; frequency>=0; frequency--) { |
| 106 | +// if (resultIndex >= result.length) { |
| 107 | +// return result; |
| 108 | +// } |
| 109 | +// |
| 110 | +// if (frequencyMap.containsKey(frequency)) { |
| 111 | +// HashSet<Integer> numElements = frequencyMap.get(frequency); |
| 112 | +// |
| 113 | +// for (int numElement : numElements) { |
| 114 | +// result[resultIndex] = numElement; |
| 115 | +// resultIndex++; |
| 116 | +// |
| 117 | +// |
| 118 | +// if (resultIndex >= result.length) { |
| 119 | +// return result; |
| 120 | +// } |
| 121 | +// } |
| 122 | +// } |
| 123 | +// } |
| 124 | +// |
| 125 | +// return result; |
| 126 | + } |
74 | 127 | } |
75 | 128 |
|
0 commit comments