From db0d430c1079c83dde1a7040171d7c907d63d269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 12 Oct 2024 14:13:52 +0900 Subject: [PATCH 1/3] Add week 9 solutions: linked-list-cycle --- linked-list-cycle/gitsunmin.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 linked-list-cycle/gitsunmin.ts diff --git a/linked-list-cycle/gitsunmin.ts b/linked-list-cycle/gitsunmin.ts new file mode 100644 index 000000000..f95d80597 --- /dev/null +++ b/linked-list-cycle/gitsunmin.ts @@ -0,0 +1,31 @@ +/** + * https://leetcode.com/problems/linked-list-cycle/ + * time complexity : O(n) + * space complexity : O(1) + */ + +export 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) + } +} + +function hasCycle(head: ListNode | null): boolean { + if (!head || !head.next) { + return false; + } + + let slow: ListNode | null = head; + let fast: ListNode | null = head.next; + + while (slow !== fast) { + if (!fast || !fast.next) return false; + slow = slow!.next; + fast = fast.next.next; + } + + return true; +}; From 1fc5dcf4d9996c4026496fa106844055e4023641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 12 Oct 2024 14:32:49 +0900 Subject: [PATCH 2/3] Add week 9 solutions: find-minimum-in-rotated-sorted-array --- .../gitsunmin.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/gitsunmin.ts diff --git a/find-minimum-in-rotated-sorted-array/gitsunmin.ts b/find-minimum-in-rotated-sorted-array/gitsunmin.ts new file mode 100644 index 000000000..8dbdef84c --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/gitsunmin.ts @@ -0,0 +1,19 @@ +/** + * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array + * time complexity : O(log n) + * space complexity : O(1) + */ + +function findMin(nums: number[]): number { + let left = 0; + let right = nums.length - 1; + + while (left < right) { + const mid = Math.floor((left + right) / 2); + + if (nums[mid] > nums[right]) left = mid + 1; + else right = mid; + } + + return nums[left]; +}; From 6ced3305af9d9336e387dbe7d4481d9e03216f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 12 Oct 2024 14:47:24 +0900 Subject: [PATCH 3/3] Add week 9 solutions: pacific-atlantic-water-flow --- pacific-atlantic-water-flow/gitsunmin.ts | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pacific-atlantic-water-flow/gitsunmin.ts diff --git a/pacific-atlantic-water-flow/gitsunmin.ts b/pacific-atlantic-water-flow/gitsunmin.ts new file mode 100644 index 000000000..71def88be --- /dev/null +++ b/pacific-atlantic-water-flow/gitsunmin.ts @@ -0,0 +1,44 @@ +/** + * https://leetcode.com/problems/two-sum + * time complexity : O(m x m) + * space complexity : O(m x n) + */ +function pacificAtlantic(heights: number[][]): number[][] { + const m = heights.length; + const n = heights[0].length; + const pacific: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); + const atlantic: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); + + const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]; + + function dfs(r: number, c: number, visited: boolean[][], prevHeight: number) { + if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || heights[r][c] < prevHeight) { + return; + } + visited[r][c] = true; + for (const [dr, dc] of directions) { + dfs(r + dr, c + dc, visited, heights[r][c]); + } + } + + for (let i = 0; i < m; i++) { + dfs(i, 0, pacific, heights[i][0]); + dfs(i, n - 1, atlantic, heights[i][n - 1]); + } + for (let i = 0; i < n; i++) { + dfs(0, i, pacific, heights[0][i]); + dfs(m - 1, i, atlantic, heights[m - 1][i]); + } + + const result: number[][] = []; + + for (let r = 0; r < m; r++) { + for (let c = 0; c < n; c++) { + if (pacific[r][c] && atlantic[r][c]) { + result.push([r, c]); + } + } + } + + return result; +}