diff --git a/coin-change/eunice-hong.ts b/coin-change/eunice-hong.ts new file mode 100644 index 000000000..3d9cabb03 --- /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]; +} 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..1e5f643ee --- /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] +}; 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..9cf35881c --- /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)) + } +}; diff --git a/merge-two-sorted-lists/eunice-hong.ts b/merge-two-sorted-lists/eunice-hong.ts new file mode 100644 index 000000000..f2535fbbc --- /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)) + } +}; diff --git a/word-search/eunice-hong.ts b/word-search/eunice-hong.ts new file mode 100644 index 000000000..143076b94 --- /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; +}