From 65cefcc2188c211c1c6a6e5f93b5b966ab91727f Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Tue, 4 Feb 2025 22:31:39 +0900 Subject: [PATCH 1/4] feat: linked-list-cycle, find-minimum-in-rotated-sorted-array solution --- .../YeomChaeeun.ts | 39 +++++++++++++++++++ linked-list-cycle/YeomChaeeun.ts | 28 +++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/YeomChaeeun.ts create mode 100644 linked-list-cycle/YeomChaeeun.ts diff --git a/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts b/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts new file mode 100644 index 000000000..446e050d1 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts @@ -0,0 +1,39 @@ +/** + * 정렬된 배열에서 최소값 찾기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(logn) + * - 공간 복잡도: O(1) + * @param nums + */ +function findMin(nums: number[]): number { + // 풀이 1 - sort() 사용 + // 시간 복잡도: O(nlogn) / 공간 복잡도: O(1) + // nums.sort((a, b) => a - b) + // return nums[0] + + // 풀이 2 - 배열이 정렬되어 있음을 활용한 풀이 + // 시간 복잡도: O(n) / 공간 복잡도: O(1) + // let min = nums[0]; + // for(let i = 1; i < nums.length; i++) { + // console.log(nums[i]) + // min = Math.min(nums[i], min) + // } + // return min + + // 이분 탐색법 활용 + // 절반씩 잘라서 nums[n-1] > nums[n] 의 지점을 찾는다 + let low = 1; + let high = nums.length - 1; + while(low <= high) { + let mid = Math.floor((low + high) / 2); + if(nums[mid - 1] > nums[mid]) { + return nums[mid]; + } + if(nums[0] < nums[mid]) { + low = mid + 1; + } else { + high = mid -1; + } + } + return nums[0] +} diff --git a/linked-list-cycle/YeomChaeeun.ts b/linked-list-cycle/YeomChaeeun.ts new file mode 100644 index 000000000..04837eb3f --- /dev/null +++ b/linked-list-cycle/YeomChaeeun.ts @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +/** + * 순환되는 링크드 리스트 찾기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(n) + * - 공간 복잡도: O(n) + * @param head + */ +function hasCycle(head: ListNode | null): boolean { + let set = new Set(); + while(head !== null) { + // set에 이미 존재하는지 확인 + if(set.has(head)) return true + set.add(head) + head = head.next + } + return false +} From 867f27f47e92be2a5b633709a889661e37c8469e Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 8 Feb 2025 21:25:26 +0900 Subject: [PATCH 2/4] fix: find-minimum-in-rotated-sorted-array --- .../YeomChaeeun.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts b/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts index 446e050d1..2b4918a44 100644 --- a/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts +++ b/find-minimum-in-rotated-sorted-array/YeomChaeeun.ts @@ -22,18 +22,16 @@ function findMin(nums: number[]): number { // 이분 탐색법 활용 // 절반씩 잘라서 nums[n-1] > nums[n] 의 지점을 찾는다 - let low = 1; + let low = 0; let high = nums.length - 1; - while(low <= high) { - let mid = Math.floor((low + high) / 2); - if(nums[mid - 1] > nums[mid]) { - return nums[mid]; - } - if(nums[0] < nums[mid]) { + while(low < high) { + let mid = low + Math.floor((high - low) / 2); + + if(nums[mid] > nums[high]) { low = mid + 1; } else { - high = mid -1; + high = mid; } } - return nums[0] + return nums[low] } From 60282fc9b4ef1db7e82d2998aeef36a9d366cdfe Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 8 Feb 2025 21:38:03 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20linked-list-cycle=20=ED=86=A0?= =?UTF-8?q?=EB=81=BC=EC=99=80=20=EA=B1=B0=EB=B6=81=EC=9D=B4=20=EC=95=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linked-list-cycle/YeomChaeeun.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/linked-list-cycle/YeomChaeeun.ts b/linked-list-cycle/YeomChaeeun.ts index 04837eb3f..3aca4c0d2 100644 --- a/linked-list-cycle/YeomChaeeun.ts +++ b/linked-list-cycle/YeomChaeeun.ts @@ -13,16 +13,33 @@ * 순환되는 링크드 리스트 찾기 * 알고리즘 복잡도 * - 시간 복잡도: O(n) - * - 공간 복잡도: O(n) + * - 공간 복잡도: O(1) * @param head */ function hasCycle(head: ListNode | null): boolean { - let set = new Set(); - while(head !== null) { - // set에 이미 존재하는지 확인 - if(set.has(head)) return true - set.add(head) - head = head.next + // 1. set을 이용한 풀이 + // 시간 복잡도: O(n) , 공간 복잡도: O(n) + // let set = new Set(); + // while(head !== null) { + // // set에 이미 존재하는지 확인 + // if(set.has(head)) return true + // set.add(head) + // head = head.next + // } + + // 2. 토끼와 거북이 알고리즘 + // 포인터를 2개 이동하는 방법 + let slow = head + let fast = head + + while (fast?.next) { + slow = slow.next + fast = fast.next.next + + if(slow === fast) { + return true + } } + return false } From 4d79e3fc212ced410e1d58f0de617bbdbe57c9d2 Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 8 Feb 2025 22:20:30 +0900 Subject: [PATCH 4/4] feat: maximum-product-subarray solution --- maximum-product-subarray/YeomChaeeun.ts | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maximum-product-subarray/YeomChaeeun.ts diff --git a/maximum-product-subarray/YeomChaeeun.ts b/maximum-product-subarray/YeomChaeeun.ts new file mode 100644 index 000000000..06a9b8431 --- /dev/null +++ b/maximum-product-subarray/YeomChaeeun.ts @@ -0,0 +1,31 @@ +/** + * 최대 부분 곱 구하기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(n) + * - 공간 복잡도: O(1) + * @param nums + */ +function maxProduct(nums: number[]): number { + if(nums.length === 1) return nums[0] + + let max = nums[0] + let currMax = nums[0] + let currMin = nums[0] + + for(let i = 1; i < nums.length; i++) { + const temp = currMax; + + currMax = Math.max( + nums[i], temp * nums[i], currMin * nums[i] + ) + + currMin = Math.min( + nums[i], temp * nums[i], currMin * nums[i] + ) + + max = Math.max(max, currMax); + } + + return max; + +}