From a33a9030bb26de0ed4c045a5bf01e2bfc0f9e9f7 Mon Sep 17 00:00:00 2001 From: Blossssom Date: Mon, 5 Jan 2026 13:10:06 +0900 Subject: [PATCH 1/4] linked-list-cycle solution --- linked-list-cycle/Blossssom.ts | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 linked-list-cycle/Blossssom.ts diff --git a/linked-list-cycle/Blossssom.ts b/linked-list-cycle/Blossssom.ts new file mode 100644 index 0000000000..6bff5f43ef --- /dev/null +++ b/linked-list-cycle/Blossssom.ts @@ -0,0 +1,53 @@ +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; + } +} + +/** + * @param head - linked list node + * @returns - 순환되는 리스트인지? + * @description + * - 풀이 1. - 단순 순회 및 조회로 판단, 결국 visit에 다시 돌아온다면 순환 + * - 풀이 2. - 1칸, 2칸 포인터를 나눠 결국 순환이라면 돌고 돌아 만나는 과정을 통한 메모리 O(1)의 풀이 + */ + +// function hasCycle(head: ListNode | null): boolean { +// const visit = new Set(); +// let current = head; + +// while (current) { +// if (visit.has(current)) { +// return true; +// } + +// visit.add(current); +// current = current.next; +// } + +// return false; +// } + +function hasCycle(head: ListNode | null): boolean { + if (!head || !head.next) { + return false; + } + + let slow: ListNode | null = head; + let fast: ListNode | null = head; + + while (fast && fast.next) { + slow = slow!.next; + fast = fast.next.next; + + if (slow === fast) { + return true; + } + } + return false; +} + + From 76c94783f004740c8c4ad9257921b6e8a7077720 Mon Sep 17 00:00:00 2001 From: Blossssom Date: Tue, 6 Jan 2026 13:41:34 +0900 Subject: [PATCH 2/4] pacific-atlantic-water-flow solution --- pacific-atlantic-water-flow/Blossssom.ts | 89 ++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 pacific-atlantic-water-flow/Blossssom.ts diff --git a/pacific-atlantic-water-flow/Blossssom.ts b/pacific-atlantic-water-flow/Blossssom.ts new file mode 100644 index 0000000000..8805438f53 --- /dev/null +++ b/pacific-atlantic-water-flow/Blossssom.ts @@ -0,0 +1,89 @@ +/** + * @param heights - 셀의 해발고도 배열 + * @returns + * @description + * - 인접한 셀의 높이가 작거나 같으면 빗물이 흐를 수 있음 + * - 바다에 인접한 셀에서는 바다로 물이 흐를 수 있음 + * - 태평양과 대서양 모두로 흐를 수 있는 경로 반환 + * - 결국 [0, n] 과 [m, 0]는 반드시 포함 + * + * 1. 역순으로 가는게 유리하다는 힌트를 채택 + * 2. 모든 셀의 이동 가능 방향을 파악 (dfs, bfs) + */ + +function pacificAtlantic(heights: number[][]): number[][] { + const yMax = heights.length; + const xMax = heights[0].length; + + // 태평양, 대서양의 visit 체크를 위한 set + const pacific = new Set(); + const atlantic = new Set(); + + const result: number[][] = []; + function dfs(y: number, x: number, visited: Set, prevHeight: number) { + const key = `${y},${x}`; + + if ( + x < 0 || + x >= xMax || + y < 0 || + y >= yMax || + visited.has(key) || + prevHeight > heights[y][x] + ) { + return; + } + + visited.add(key); + dfs(y, x + 1, visited, heights[y][x]); + dfs(y, x - 1, visited, heights[y][x]); + dfs(y + 1, x, visited, heights[y][x]); + dfs(y - 1, x, visited, heights[y][x]); + } + + // 맨 양 끝쪽을 dfs의 시작점으로 지정 + for (let i = 0; i < yMax; i++) { + dfs(i, 0, pacific, heights[i][0]); + dfs(i, xMax - 1, atlantic, heights[i][xMax - 1]); + } + + for (let j = 0; j < xMax; j++) { + dfs(0, j, pacific, heights[0][j]); + dfs(yMax - 1, j, atlantic, heights[yMax - 1][j]); + } + + // 유니온 값 체크 + for (let i = 0; i < yMax; i++) { + for (let j = 0; j < xMax; j++) { + const key = `${i},${j}`; + + if (pacific.has(key) && atlantic.has(key)) { + result.push([i, j]); + } + } + } + + return result; +} + +const heights = [ + [1, 2, 2, 3, 5], + [3, 2, 3, 4, 4], + [2, 4, 5, 3, 1], + [6, 7, 1, 4, 5], + [5, 1, 1, 2, 4], +]; + +pacificAtlantic(heights); + +// 태 평 양 (Pacific) +// ~ ~ ~ ~ ~ ~ +// ~ [1] [2] [2] [3] (5) ~ <-- 오른쪽/아래는 대서양 +// ~ [3] [2] [3] (4) (4) ~ +// ~ [2] [4] (5) [3] [1] ~ +// ~ (6) (7) (1) [4] [5] ~ +// ~ (5) [1] [1] [2] [4] ~ +// ~ ~ ~ ~ ~ ~ +// 대 서 양 (Atlantic) + + From 9d90baa304c11efa0cf79c825649406f25ff8390 Mon Sep 17 00:00:00 2001 From: Blossssom Date: Fri, 9 Jan 2026 17:52:37 +0900 Subject: [PATCH 3/4] maximum-product-subarray solution --- maximum-product-subarray/Blossssom.ts | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 maximum-product-subarray/Blossssom.ts diff --git a/maximum-product-subarray/Blossssom.ts b/maximum-product-subarray/Blossssom.ts new file mode 100644 index 0000000000..8b074c35fe --- /dev/null +++ b/maximum-product-subarray/Blossssom.ts @@ -0,0 +1,37 @@ +/** + * @param nums - 정수 배열 + * @returns - 정수 배열의 부분 배열의 곱이 가장 큰 값을 반환 + * @description + * - 값이 음수일 경우 최솟값과 곱해 최댓값을 도출 할 수 있으므로 swap + * - min, max를 현재 값과 비교하며 변경 + */ + +function maxProduct(nums: number[]): number { + let maximum = nums[0]; + let minimum = nums[0]; + let result = nums[0]; + + for (let i = 1; i < nums.length; i++) { + const current = nums[i]; + + if (current < 0) { + let temp = maximum; + maximum = minimum; + minimum = temp; + } + + maximum = Math.max(current, current * maximum); + minimum = Math.min(current, current * minimum); + + result = Math.max(result, maximum); + } + + return result; +} + +const nums = [-2, 3, -4]; + +maxProduct(nums); + + + From 718ac71ce538dd7464ec1d7c0cea716d4d4f364e Mon Sep 17 00:00:00 2001 From: Blossssom Date: Fri, 9 Jan 2026 18:28:21 +0900 Subject: [PATCH 4/4] sum-of-two-integers solution --- sum-of-two-integers/Blossssom.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sum-of-two-integers/Blossssom.ts diff --git a/sum-of-two-integers/Blossssom.ts b/sum-of-two-integers/Blossssom.ts new file mode 100644 index 0000000000..23a923ab97 --- /dev/null +++ b/sum-of-two-integers/Blossssom.ts @@ -0,0 +1,28 @@ +/** + * + * @param a - 정수 a + * @param b - 정수 b + * @returns - 더하기 연산 없이 더한 값 반환 + * @description + * - bit 연산이구나! 싶었는데 비트를 잘 다룰줄 몰라서 ai의 도움을 받음 + */ + +function getSum(a: number, b: number): number { + while (b !== 0) { + // 올림수 계산 : 둘다 1인 비트를 AND 연산으로 찾아 왼쪽으로 밈 (올림수니까) + const carry = (a & b) << 1; + + // XOR 연산으로 올림수를 제외한 덧셈 값 도출 (1 | 0 이므로) + a = a ^ b; + + b = carry; + } + + return a; +} + +const a = 2; +const b = 3; +getSum(a, b); + +