diff --git a/coin-change/hyer0705.ts b/coin-change/hyer0705.ts new file mode 100644 index 0000000000..4cee4f0104 --- /dev/null +++ b/coin-change/hyer0705.ts @@ -0,0 +1,17 @@ +function coinChange(coins: number[], amount: number): number { + // dp[i] = i 금액을 만드는 데 필요한 최소 동전의 갯수... + const dp: number[] = Array(amount + 1).fill(Infinity); + dp[0] = 0; + + for (let i = 1; i <= amount; i++) { + let min = Infinity; + for (const coin of coins) { + if (i >= coin) { + min = Math.min(dp[i - coin] + 1, min); + } + } + dp[i] = min; + } + + return Number.isFinite(dp[amount]) ? dp[amount] : -1; +} diff --git a/find-minimum-in-rotated-sorted-array/hyer0705.ts b/find-minimum-in-rotated-sorted-array/hyer0705.ts new file mode 100644 index 0000000000..9714d32be1 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/hyer0705.ts @@ -0,0 +1,18 @@ +function findMin(nums: number[]): number { + const n = nums.length; + + let l = 0; + let r = n - 1; + + while (l < r) { + const mid = Math.floor((l + r) / 2); + + if (nums[r] >= nums[mid]) { + r = mid; + } else if (nums[r] < nums[mid]) { + l = mid + 1; + } + } + + return nums[l]; +} diff --git a/maximum-depth-of-binary-tree/hyer0705.ts b/maximum-depth-of-binary-tree/hyer0705.ts new file mode 100644 index 0000000000..949af17d6d --- /dev/null +++ b/maximum-depth-of-binary-tree/hyer0705.ts @@ -0,0 +1,19 @@ +/** + * 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) + * } + * } + */ + +function maxDepth(root: TreeNode | null): number { + if (!root) return 0; + + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); +} diff --git a/merge-two-sorted-lists/hyer0705.ts b/merge-two-sorted-lists/hyer0705.ts new file mode 100644 index 0000000000..cfa0004136 --- /dev/null +++ b/merge-two-sorted-lists/hyer0705.ts @@ -0,0 +1,44 @@ +/** + * 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) + * } + * } + */ + +function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { + if (!list1 && !list2) return null; + + if (!list1) return list2; + if (!list2) return list1; + + const dummyHead = new ListNode(-101); + let tail = dummyHead; + + let pointerOne: ListNode | null = list1; + let pointerTwo: ListNode | null = list2; + + while (pointerOne && pointerTwo) { + if (pointerOne.val <= pointerTwo.val) { + tail.next = pointerOne; + pointerOne = pointerOne.next; + } else { + tail.next = pointerTwo; + pointerTwo = pointerTwo.next; + } + + tail = tail.next; + } + + if (pointerOne) { + tail.next = pointerOne; + } else { + tail.next = pointerTwo; + } + + return dummyHead.next; +} diff --git a/word-search/hyer0705.ts b/word-search/hyer0705.ts new file mode 100644 index 0000000000..6433d266cd --- /dev/null +++ b/word-search/hyer0705.ts @@ -0,0 +1,48 @@ +function exist(board: string[][], word: string): boolean { + const m = board.length; + const n = board[0].length; + + const visited: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); + + const isValidCoordinates = (index: number, x: number, y: number): boolean => { + if (x >= 0 && x < m && y >= 0 && y < n && word[index] === board[x][y] && !visited[x][y]) return true; + return false; + }; + + const backtrack = (index: number, cx: number, cy: number): boolean => { + if (index === word.length - 1 && board[cx][cy] === word[index]) return true; + + if (board[cx][cy] !== word[index]) return false; + + for (const [dx, dy] of [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + ]) { + const [nx, ny] = [cx + dx, cy + dy]; + + if (isValidCoordinates(index + 1, nx, ny)) { + visited[nx][ny] = true; + const result = backtrack(index + 1, nx, ny); + if (result) return true; + visited[nx][ny] = false; + } + } + + return false; + }; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (board[i][j] === word[0]) { + visited[i][j] = true; + const result: boolean = backtrack(0, i, j); + if (result) return true; + visited[i][j] = false; + } + } + } + + return false; +}