From 37fa2b20f4234db06273db146c4b0e0a1f644343 Mon Sep 17 00:00:00 2001 From: JIA Date: Sat, 15 Nov 2025 11:22:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?-=201=EC=A3=BC=EC=B0=A8=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/Geegong.java | 15 +- house-robber/Geegong.java | 50 ++++++- longest-consecutive-sequence/Geegong.java | 105 ++++++++++---- top-k-frequent-elements/Geegong.java | 162 ++++++++++++++-------- two-sum/Geegong.java | 44 ++++-- 5 files changed, 280 insertions(+), 96 deletions(-) diff --git a/contains-duplicate/Geegong.java b/contains-duplicate/Geegong.java index 348e341ae..b70052a8c 100644 --- a/contains-duplicate/Geegong.java +++ b/contains-duplicate/Geegong.java @@ -12,14 +12,23 @@ public class Geegong { * @return */ public boolean containsDuplicate(int[] nums) { + + + HashSet uniques = new HashSet<>(); for (int num : nums) { - if (uniques.contains(num)) { + + // 명확하게 hashSet에 값이 있는지 체크하는 메소드로 확인이 가능하지만 +// if (uniques.contains(num)) { +// return true; +// } +// uniques.add(num); + + // hashSet 의 Add 는 이미 값이 있다면 FALSE를 리턴하기에 아래처럼도 동작 가능 (더 빠른 결과확인) + if (!uniques.add(num)) { return true; } - - uniques.add(num); } return false; diff --git a/house-robber/Geegong.java b/house-robber/Geegong.java index 0fa2a7832..8f2f71eb6 100644 --- a/house-robber/Geegong.java +++ b/house-robber/Geegong.java @@ -1,4 +1,52 @@ +import java.util.HashMap; +import java.util.Map; + public class Geegong { - // 이 문제는 시간이 남을때 풀 예정 😅 + + /** + * top-down + memoization 방식으로 풀이 + * memoization (memo 변수) 없이 풀이하면 Time Limit Exceeded 발생 + * time complexity : O(N) -> memo 가 있어서 이미 연산이 된건 패스함 + * space complexity : O(N) -> index 만큼의 연산 결과가 있음 + * @param nums + * @return + */ + public int rob(int[] nums) { + // memoization 하지 않으면 Time Limit Exceeded. + Map memo = new HashMap<>(); + + int maxAmount = 0; + for (int idx=0; idx memo) { + if (currIdx == origin.length - 1) { + return origin[currIdx]; + } else if (currIdx >= origin.length) { // when out of bounds + return 0; + } + + if (memo.containsKey(currIdx)) { + return memo.get(currIdx); + } + + int currentVal = origin[currIdx]; + + int maxAmount = Math.max( + currentVal + rob(origin, currIdx + 2, memo), rob(origin, currIdx+1, memo)); + + memo.put(currIdx, maxAmount); + + return maxAmount; + } + + } diff --git a/longest-consecutive-sequence/Geegong.java b/longest-consecutive-sequence/Geegong.java index 2a0e5624e..ccf44b2cf 100644 --- a/longest-consecutive-sequence/Geegong.java +++ b/longest-consecutive-sequence/Geegong.java @@ -1,3 +1,4 @@ +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -7,43 +8,99 @@ */ public class Geegong { + /** + * Time complexity : O(N) + O(N long N) + O(N) + * - o(N) : 한번 순회해서 set + * - O(N log N) : sorting + * - O (N) : sorting된걸 한번 더 순회 + * Space complexity : O(N) -> hash set + * @param nums + * @return + */ public int longestConsecutive(int[] nums) { - HashSet setOfNums = new HashSet<>(); - // key : startIndex , value : length - Map lengthMap = new HashMap<>(); - - // sort..? 를 해야될까 싶음.. + if (nums.length == 0) { + return 0; + } - // initialize + // hashSet + HashSet hashSet = new HashSet<>(); for (int num : nums) { - setOfNums.add(num); + hashSet.add(num); } - Integer longest = 0; + int[] sortedNums = hashSet.stream().mapToInt(val -> val).sorted().toArray(); - for (Integer num : setOfNums) { - int length = iterate(setOfNums, num, 0, lengthMap); - longest = Math.max(longest, length); - } + int maxLength = 1; + int currentLength = 1; + int prev = sortedNums[0]; + for(int index=1; index hashSet, int currIndex, int currLength, Map lengthMap) { - if (lengthMap.containsKey(currIndex)) { - return lengthMap.get(currIndex); + prev = sortedNums[index]; } - if (hashSet.contains(currIndex)) { - currLength++; - return iterate(hashSet, currIndex+1, currLength, lengthMap); - } else { - lengthMap.put(currIndex, currLength); - return currLength; - } + + return Math.max(currentLength, maxLength); + + + + + + + + + + + + + + } +// HashSet setOfNums = new HashSet<>(); +// // key : startIndex , value : length +// Map lengthMap = new HashMap<>(); +// +// // sort..? 를 해야될까 싶음.. +// +// // initialize +// for (int num : nums) { +// setOfNums.add(num); +// } +// +// Integer longest = 0; +// +// for (Integer num : setOfNums) { +// int length = iterate(setOfNums, num, 0, lengthMap); +// longest = Math.max(longest, length); +// } +// +// return longest; +// } +// +// public Integer iterate(HashSet hashSet, int currIndex, int currLength, Map lengthMap) { +// if (lengthMap.containsKey(currIndex)) { +// return lengthMap.get(currIndex); +// } +// +// if (hashSet.contains(currIndex)) { +// currLength++; +// return iterate(hashSet, currIndex+1, currLength, lengthMap); +// +// } else { +// lengthMap.put(currIndex, currLength); +// return currLength; +// } +// +// } } diff --git a/top-k-frequent-elements/Geegong.java b/top-k-frequent-elements/Geegong.java index 4770f5422..297c67f9c 100644 --- a/top-k-frequent-elements/Geegong.java +++ b/top-k-frequent-elements/Geegong.java @@ -1,75 +1,127 @@ -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; +import java.util.*; -public class Geegong { - public int[] topKFrequent(int[] nums, int k) { - int[] result = new int[k]; +public class Geegong { - // key : num element in nums / value : frequency of num elements - Map numMap = new HashMap<>(); - // key : frequency of num elements / value : HashSet num elements - Map> frequencyMap = new HashMap<>(); + /** + * Map 으로 빈도수를 key , 빈도수에 해당되는 num 들을 list 로 저장 + * key (빈도수) 를 sorting + * k 만큼 골라낸다 + * Time Complexity : O(N) + O(N logN) + O(N) + * - O(N) : nums 만큼 iterate + * - O(N log N) : sorting + * - O(N) : frequency 그룹핑된 그룹 갯수만큼 iterate + * @param nums + * @param k + * @return + */ + public int[] topKFrequent(int[] nums, int k) { - // most frequent numbers - int maxCount = 0; + // key : frequency , value : list of nums + Map> map = new HashMap<>(); + Arrays.sort(nums); + int current = nums[0]; + int count = 1; - // initialize numMap - for (int num : nums) { - if (numMap.containsKey(num)) { - Integer alreadyCounted = numMap.get(num); - numMap.put(num, alreadyCounted + 1); + for (int i = 1; i < nums.length; i++) { + if (nums[i] == current) { + count++; } else { - numMap.put(num, 1); + map.computeIfAbsent(count, el -> new ArrayList<>()).add(current); + current = nums[i]; + count = 1; } } + // Add last group + map.computeIfAbsent(count, el -> new ArrayList<>()).add(current); - //numHashSetMap - for (int num : numMap.keySet()) { - int frequencyOfNum = numMap.get(num); - maxCount = Math.max(maxCount, frequencyOfNum); - - if (frequencyMap.containsKey(frequencyOfNum)) { - HashSet alreadySet = frequencyMap.get(frequencyOfNum); - alreadySet.add(num); - - frequencyMap.put(frequencyOfNum, alreadySet); - - } else { - HashSet newHashSet = new HashSet<>(); - newHashSet.add(num); - - frequencyMap.put(frequencyOfNum, newHashSet); - } - } + List sortedFrequency = map.keySet().stream().sorted(Comparator.reverseOrder()).toList(); + List result = new ArrayList<>(); + for (int index=0; index numsOfFreq = map.get(mostFrequency); + for(int innerIndex = 0; innerIndex=0; frequency--) { - if (resultIndex >= result.length) { - return result; + result.add(numsOfFreq.get(innerIndex)); } - if (frequencyMap.containsKey(frequency)) { - HashSet numElements = frequencyMap.get(frequency); - - for (int numElement : numElements) { - result[resultIndex] = numElement; - resultIndex++; - - - if (resultIndex >= result.length) { - return result; - } - } - } } - return result; + return result.stream().mapToInt(Integer::intValue).toArray(); + + +// int[] result = new int[k]; +// +// // key : num element in nums / value : frequency of num elements +// Map numMap = new HashMap<>(); +// +// // key : frequency of num elements / value : HashSet num elements +// Map> frequencyMap = new HashMap<>(); +// +// // most frequent numbers +// int maxCount = 0; +// +// // initialize numMap +// for (int num : nums) { +// if (numMap.containsKey(num)) { +// Integer alreadyCounted = numMap.get(num); +// numMap.put(num, alreadyCounted + 1); +// } else { +// numMap.put(num, 1); +// } +// } +// +// +// //numHashSetMap +// for (int num : numMap.keySet()) { +// int frequencyOfNum = numMap.get(num); +// maxCount = Math.max(maxCount, frequencyOfNum); +// +// if (frequencyMap.containsKey(frequencyOfNum)) { +// HashSet alreadySet = frequencyMap.get(frequencyOfNum); +// alreadySet.add(num); +// +// frequencyMap.put(frequencyOfNum, alreadySet); +// +// } else { +// HashSet newHashSet = new HashSet<>(); +// newHashSet.add(num); +// +// frequencyMap.put(frequencyOfNum, newHashSet); +// } +// } +// +// +// // maxCount 부터 decreasing +// int resultIndex=0; +// for(int frequency=maxCount; frequency>=0; frequency--) { +// if (resultIndex >= result.length) { +// return result; +// } +// +// if (frequencyMap.containsKey(frequency)) { +// HashSet numElements = frequencyMap.get(frequency); +// +// for (int numElement : numElements) { +// result[resultIndex] = numElement; +// resultIndex++; +// +// +// if (resultIndex >= result.length) { +// return result; +// } +// } +// } +// } +// +// return result; + } } diff --git a/two-sum/Geegong.java b/two-sum/Geegong.java index 3fd371057..0c5bff5c9 100644 --- a/two-sum/Geegong.java +++ b/two-sum/Geegong.java @@ -11,26 +11,44 @@ public class Geegong { * @return */ public int[] twoSum(int[] nums, int target) { - Map map = new HashMap<>(); int[] result = new int[2]; + // key : value, value = index + Map maps = new HashMap(); - // if target = -9 / num = 1 , num = -10 - for (int index=0; index map = new HashMap<>(); +// int[] result = new int[2]; +// +// // if target = -9 / num = 1 , num = -10 +// for (int index=0; index Date: Sun, 16 Nov 2025 09:48:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?-=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/Geegong.java | 2 +- longest-consecutive-sequence/Geegong.java | 55 +---------------------- top-k-frequent-elements/Geegong.java | 5 ++- two-sum/Geegong.java | 24 +--------- 4 files changed, 6 insertions(+), 80 deletions(-) diff --git a/contains-duplicate/Geegong.java b/contains-duplicate/Geegong.java index b70052a8c..e010bdadf 100644 --- a/contains-duplicate/Geegong.java +++ b/contains-duplicate/Geegong.java @@ -9,7 +9,7 @@ public class Geegong { * time complexity : O(n) * space complexity : o(n) * @param nums - * @return + * @return boolean */ public boolean containsDuplicate(int[] nums) { diff --git a/longest-consecutive-sequence/Geegong.java b/longest-consecutive-sequence/Geegong.java index ccf44b2cf..61fa2d51a 100644 --- a/longest-consecutive-sequence/Geegong.java +++ b/longest-consecutive-sequence/Geegong.java @@ -15,7 +15,7 @@ public class Geegong { * - O (N) : sorting된걸 한번 더 순회 * Space complexity : O(N) -> hash set * @param nums - * @return + * @return int */ public int longestConsecutive(int[] nums) { if (nums.length == 0) { @@ -46,61 +46,8 @@ public int longestConsecutive(int[] nums) { prev = sortedNums[index]; } - - return Math.max(currentLength, maxLength); - - - - - - - - - - - - - - - } -// HashSet setOfNums = new HashSet<>(); -// // key : startIndex , value : length -// Map lengthMap = new HashMap<>(); -// -// // sort..? 를 해야될까 싶음.. -// -// // initialize -// for (int num : nums) { -// setOfNums.add(num); -// } -// -// Integer longest = 0; -// -// for (Integer num : setOfNums) { -// int length = iterate(setOfNums, num, 0, lengthMap); -// longest = Math.max(longest, length); -// } -// -// return longest; -// } -// -// public Integer iterate(HashSet hashSet, int currIndex, int currLength, Map lengthMap) { -// if (lengthMap.containsKey(currIndex)) { -// return lengthMap.get(currIndex); -// } -// -// if (hashSet.contains(currIndex)) { -// currLength++; -// return iterate(hashSet, currIndex+1, currLength, lengthMap); -// -// } else { -// lengthMap.put(currIndex, currLength); -// return currLength; -// } -// -// } } diff --git a/top-k-frequent-elements/Geegong.java b/top-k-frequent-elements/Geegong.java index 297c67f9c..9d27980ef 100644 --- a/top-k-frequent-elements/Geegong.java +++ b/top-k-frequent-elements/Geegong.java @@ -15,7 +15,7 @@ public class Geegong { * - O(N) : frequency 그룹핑된 그룹 갯수만큼 iterate * @param nums * @param k - * @return + * @return int[] */ public int[] topKFrequent(int[] nums, int k) { @@ -56,7 +56,8 @@ public int[] topKFrequent(int[] nums, int k) { return result.stream().mapToInt(Integer::intValue).toArray(); - +// 아래 문제풀이는 예전 기수에 풀었던 방법으로 Map 으로 빈도수와 num을 관리하는 값을 가지긴 하나 +// sorting은 하지 않고 maxNumOfFrequency를 구하여 순차적으로 작은 값들을 꺼내서 k만큼 리턴한다 // int[] result = new int[k]; // // // key : num element in nums / value : frequency of num elements diff --git a/two-sum/Geegong.java b/two-sum/Geegong.java index 0c5bff5c9..3dd411549 100644 --- a/two-sum/Geegong.java +++ b/two-sum/Geegong.java @@ -8,7 +8,7 @@ public class Geegong { * space complexity : O(n) * @param nums * @param target - * @return + * @return int[] */ public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; @@ -27,28 +27,6 @@ public int[] twoSum(int[] nums, int target) { } return result; - - -// Map map = new HashMap<>(); -// int[] result = new int[2]; -// -// // if target = -9 / num = 1 , num = -10 -// for (int index=0; index