diff --git a/3sum/Geegong.java b/3sum/Geegong.java new file mode 100644 index 000000000..83fbd59d6 --- /dev/null +++ b/3sum/Geegong.java @@ -0,0 +1,82 @@ +import java.util.*; + +public class Geegong { + + /** + * Time complexity : O(n^2) + * space complexity : O(n^2) + * @param nums + * @return + */ + public List> threeSum(int[] nums) { + + // 중복되는 값은 없어야 하기에 HashSet 으로 result + HashSet> result = new HashSet<>(); + + // Key : 배열 원소 , value : List 인덱스들 + // elementMap 은 two pointer 의 값을 더한 값에서 0이 되기 위한 요소를 찾기위해 사용될 것임 + // tc : O(n) + Map> elementMap = new HashMap<>(); + for (int index = 0; index indices = elementMap.get(value); + indices.add(index); + elementMap.put(value, indices); + } else { + List newIndices = new ArrayList<>(); + newIndices.add(index); + elementMap.put(value, newIndices); + } + } + + // leftIndex : 0에서 부터 시작하는 index + // rightIndex : nums.length - 1에서부터 감소하는 index + // leftIndex > rightIndex 되는 순간까지만 for문을 돌 것이다. + // tc : O(N^2 / 2) + for (int leftIndex=0; leftIndex < nums.length; leftIndex++) { + for (int rightIndex=nums.length - 1; rightIndex >= 0; rightIndex--) { + + if (leftIndex >= rightIndex) { + break; + } + + + int leftValue = nums[leftIndex]; + int rightValue = nums[rightIndex]; + + int neededValueToZero = -leftValue - rightValue; + if (elementMap.containsKey(neededValueToZero)) { + // elementMap의 value 가 leftIndex, rightIndex 은 아닌지 확인 + + List indices = elementMap.get(neededValueToZero); + // zero 를 만들 수 있는 세번쨰 인덱스가 있는지 확인 + int thirdIndex = findThirdIndexToBeZero(leftIndex, rightIndex, indices); + if (-1 != thirdIndex) { + List newOne = new ArrayList<>(); + newOne.add(nums[leftIndex]); + newOne.add(nums[rightIndex]); + newOne.add(nums[thirdIndex]); + result.add(newOne.stream().sorted().toList()); + } + + } + + } + } + + return result.stream().toList(); + + } + + public int findThirdIndexToBeZero(int leftIndex, int rightIndex, List indices) { + for (int index : indices) { + if (index != leftIndex && index != rightIndex) { + return index; + } + } + + return -1; + } +} + diff --git a/house-robber/Geegong.java b/house-robber/Geegong.java new file mode 100644 index 000000000..0fa2a7832 --- /dev/null +++ b/house-robber/Geegong.java @@ -0,0 +1,4 @@ +public class Geegong { + // 이 문제는 시간이 남을때 풀 예정 😅 +} + diff --git a/product-of-array-except-self/Geegong.java b/product-of-array-except-self/Geegong.java new file mode 100644 index 000000000..06f01a071 --- /dev/null +++ b/product-of-array-except-self/Geegong.java @@ -0,0 +1,51 @@ +import java.util.Arrays; + +public class Geegong { + + /** + * Time complexity : O(n) + * Space complexity : O(1) (except result) + * + * 풀이 : prefix array , suffix array 를 각 배열마다 구하여 각 array에 해당되는 인덱스의 값들을 곱하여 결과값을 도출 + * + * @param nums + * @return + */ + public int[] productExceptSelf(int[] nums) { + + int[] result = new int[nums.length]; + + // 1. result 는 먼저 prefix 배열들의 곱으로 채운다. + // ( prefix 배열이란 해당되는 index 이전의 배열요소값들을 의미) + + // 앞에서부터 누적되는 총 곱의 값 + int accumulatedProduct = 1; + for (int index=0; index= 0; index--) { + if (index == nums.length - 1) { + accumulatedProduct = nums[index]; + continue; + } + + result[index] = result[index] * accumulatedProduct; + accumulatedProduct = accumulatedProduct * nums[index]; + } + + return result; + } + +} +