From 70307dc4ccab73a779e44f82e9d3a8d7fe8f71e7 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Mon, 21 Apr 2025 22:31:11 +0900 Subject: [PATCH 1/6] solve: W04 merge two sorted list --- merge-two-sorted-lists/eunice-hong.ts | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 merge-two-sorted-lists/eunice-hong.ts diff --git a/merge-two-sorted-lists/eunice-hong.ts b/merge-two-sorted-lists/eunice-hong.ts new file mode 100644 index 000000000..7dd1fde23 --- /dev/null +++ b/merge-two-sorted-lists/eunice-hong.ts @@ -0,0 +1,34 @@ +/** + * 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) + * } + * } + */ +/** + * + * Time Complexity: O(n + m) + * Space Complexity: O(n + m) + */ +function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { + if (!list1 && !list2) { + // both lists are empty + return null + } else if (!list1) { + // list1 is empty + return list2 + } else if (!list2) { + // list2 is empty + return list1 + } else if (list1.val <= list2.val) { + // list1's current node is smaller + return new ListNode(list1.val, mergeTwoLists(list1.next, list2)) + } else { + // list2's current node is smaller + return new ListNode(list2.val, mergeTwoLists(list1, list2.next)) + } +}; \ No newline at end of file From 90ecae0b37070107b7505dd77b1a6b1bbcce30d1 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Mon, 21 Apr 2025 22:35:46 +0900 Subject: [PATCH 2/6] solve: W04 maximum depth of binary tree --- maximum-depth-of-binary-tree/eunice-hong.ts | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maximum-depth-of-binary-tree/eunice-hong.ts diff --git a/maximum-depth-of-binary-tree/eunice-hong.ts b/maximum-depth-of-binary-tree/eunice-hong.ts new file mode 100644 index 000000000..af38078d7 --- /dev/null +++ b/maximum-depth-of-binary-tree/eunice-hong.ts @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +/** + * + * Time Complexity: O(n) + * Space Complexity: O(n) + */ +function maxDepth(root: TreeNode | null): number { + if (!root) { + // tree is empty + return 0 + } else if (!root.left && !root.right) { + // tree has no children + return 1 + } else { + // tree has at least one child + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)) + } +}; \ No newline at end of file From ea4023be533ad2419f64a737fba6c5dcdc1276d2 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Mon, 21 Apr 2025 22:47:55 +0900 Subject: [PATCH 3/6] solve: W04 find minimum in rotated sorted array --- .../eunice-hong.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/eunice-hong.ts diff --git a/find-minimum-in-rotated-sorted-array/eunice-hong.ts b/find-minimum-in-rotated-sorted-array/eunice-hong.ts new file mode 100644 index 000000000..dc4cff965 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/eunice-hong.ts @@ -0,0 +1,15 @@ + +/** + * + * Time Complexity: O(n) + * Space Complexity: O(1) + */ +function findMin(nums: number[]): number { + let index = 0; + // find the index of where the value is greater than the next value + while (nums[index] < nums[(index + 1) % nums.length]) { + index++; + } + // return the next value + return nums[(index + 1) % nums.length] +}; \ No newline at end of file From 3d1166a90127bc22e658b41cd54fcf5bbd68a735 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Fri, 25 Apr 2025 22:12:46 +0900 Subject: [PATCH 4/6] solve: W04 coin change --- coin-change/eunice-hong.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 coin-change/eunice-hong.ts diff --git a/coin-change/eunice-hong.ts b/coin-change/eunice-hong.ts new file mode 100644 index 000000000..0bc05845c --- /dev/null +++ b/coin-change/eunice-hong.ts @@ -0,0 +1,24 @@ + +/** + * + * Time Complexity: O(n * amount) + * Space Complexity: O(amount) + */ +function coinChange(coins: number[], amount: number): number { + // Initialize dp array with Infinity + const dp = new Array(amount + 1).fill(Infinity); + // Base case: 0 coins needed to make amount 0 + dp[0] = 0; + + // Iterate through each coin + for (let coin of coins) { + // Update dp array for each amount from coin to amount + for (let i = coin; i <= amount; i++) { + // Choose the minimum between current dp[i] and dp[i - coin] + 1 + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + + // If dp[amount] is still Infinity, it means it's not possible to make the amount + return dp[amount] === Infinity ? -1 : dp[amount]; +} \ No newline at end of file From 9e2396dfce7216ce9aa0fff0653f092442b583bb Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Fri, 25 Apr 2025 22:24:58 +0900 Subject: [PATCH 5/6] solve: W04 word search --- word-search/eunice-hong.ts | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 word-search/eunice-hong.ts diff --git a/word-search/eunice-hong.ts b/word-search/eunice-hong.ts new file mode 100644 index 000000000..5922238dc --- /dev/null +++ b/word-search/eunice-hong.ts @@ -0,0 +1,45 @@ + +/** + * Time Complexity: O(m * n * 4^k) + * Space Complexity: O(m * n) + */ +function exist(board: string[][], word: string): boolean { + // Initialize the board dimensions + const m = board.length; + const n = board[0].length; + + const dfs = (i: number, j: number, index: number): boolean => { + // If we've matched all characters, return true + if (index === word.length) return true; + // If out of bounds or current character doesn't match, return false + if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] !== word[index]) return false; + + // Temporarily mark the current cell as visited + const temp = board[i][j]; + board[i][j] = '#'; + + // Explore all four possible directions + const found = + dfs(i + 1, j, index + 1) || + dfs(i - 1, j, index + 1) || + dfs(i, j + 1, index + 1) || + dfs(i, j - 1, index + 1); + + // Unmark the current cell (backtracking) + board[i][j] = temp; + + // Return true if we found the word + return found; + }; + + // Iterate through each cell in the board + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + // Start DFS from each cell + if (dfs(i, j, 0)) return true; + } + } + + // If we didn't find the word, return false + return false; +} \ No newline at end of file From 4c9c3c1e891d38174a5a1a513b68693aa0864bfb Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Fri, 25 Apr 2025 22:28:11 +0900 Subject: [PATCH 6/6] chore: Add missing newline at end of file in multiple solution files --- coin-change/eunice-hong.ts | 2 +- find-minimum-in-rotated-sorted-array/eunice-hong.ts | 2 +- maximum-depth-of-binary-tree/eunice-hong.ts | 2 +- merge-two-sorted-lists/eunice-hong.ts | 2 +- word-search/eunice-hong.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/coin-change/eunice-hong.ts b/coin-change/eunice-hong.ts index 0bc05845c..3d9cabb03 100644 --- a/coin-change/eunice-hong.ts +++ b/coin-change/eunice-hong.ts @@ -21,4 +21,4 @@ function coinChange(coins: number[], amount: number): number { // If dp[amount] is still Infinity, it means it's not possible to make the amount return dp[amount] === Infinity ? -1 : dp[amount]; -} \ No newline at end of file +} diff --git a/find-minimum-in-rotated-sorted-array/eunice-hong.ts b/find-minimum-in-rotated-sorted-array/eunice-hong.ts index dc4cff965..1e5f643ee 100644 --- a/find-minimum-in-rotated-sorted-array/eunice-hong.ts +++ b/find-minimum-in-rotated-sorted-array/eunice-hong.ts @@ -12,4 +12,4 @@ function findMin(nums: number[]): number { } // return the next value return nums[(index + 1) % nums.length] -}; \ No newline at end of file +}; diff --git a/maximum-depth-of-binary-tree/eunice-hong.ts b/maximum-depth-of-binary-tree/eunice-hong.ts index af38078d7..9cf35881c 100644 --- a/maximum-depth-of-binary-tree/eunice-hong.ts +++ b/maximum-depth-of-binary-tree/eunice-hong.ts @@ -28,4 +28,4 @@ function maxDepth(root: TreeNode | null): number { // tree has at least one child return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)) } -}; \ No newline at end of file +}; diff --git a/merge-two-sorted-lists/eunice-hong.ts b/merge-two-sorted-lists/eunice-hong.ts index 7dd1fde23..f2535fbbc 100644 --- a/merge-two-sorted-lists/eunice-hong.ts +++ b/merge-two-sorted-lists/eunice-hong.ts @@ -31,4 +31,4 @@ function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode // list2's current node is smaller return new ListNode(list2.val, mergeTwoLists(list1, list2.next)) } -}; \ No newline at end of file +}; diff --git a/word-search/eunice-hong.ts b/word-search/eunice-hong.ts index 5922238dc..143076b94 100644 --- a/word-search/eunice-hong.ts +++ b/word-search/eunice-hong.ts @@ -42,4 +42,4 @@ function exist(board: string[][], word: string): boolean { // If we didn't find the word, return false return false; -} \ No newline at end of file +}