diff --git a/coin-change/hoyeongkwak.ts b/coin-change/hoyeongkwak.ts new file mode 100644 index 000000000..fbde2cea3 --- /dev/null +++ b/coin-change/hoyeongkwak.ts @@ -0,0 +1,20 @@ +function coinChange(coins: number[], amount: number): number { + /* + O(n) + O(n) + */ + if (amount === 0) return 0 + + const dp = new Array(amount + 1).fill(Infinity) + dp[0] = 0 + for(let coin of coins) { + for (let idx = coin; idx <= amount; idx++) { + dp[idx] = Math.min(dp[idx], dp[idx - coin] + 1) + } + } + if (dp[amount] === Infinity) { + return -1 + } else { + return dp[amount] + } +}; diff --git a/find-minimum-in-rotated-sorted-array/hoyeongkwak.ts b/find-minimum-in-rotated-sorted-array/hoyeongkwak.ts new file mode 100644 index 000000000..e3bb7756f --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/hoyeongkwak.ts @@ -0,0 +1,24 @@ +function findMin(nums: number[]): number { + /* + space complexity: 1 + time complexity : nlogn + */ + // return nums.sort((a, b) => a - b)[0] + + /* + space complexity: 1 + time complexity : logn + */ + let left = 0 + let right = nums.length - 1 + + while (left < right) { + const mid = left + Math.floor((right - left) / 2) + if (nums[mid] > nums[right]) { + left = mid + 1 + } else { + right = mid + } + } + return nums[left] +}; diff --git a/maximum-depth-of-binary-tree/hoyeongkwak.ts b/maximum-depth-of-binary-tree/hoyeongkwak.ts new file mode 100644 index 000000000..7dba649db --- /dev/null +++ b/maximum-depth-of-binary-tree/hoyeongkwak.ts @@ -0,0 +1,25 @@ +/** + * 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(logn) +*/ + +function maxDepth(root: TreeNode | null): number { + if (root == null) return 0 + let maxLeft = maxDepth(root.left) + let maxRight = maxDepth(root.right) + return Math.max(maxLeft, maxRight) + 1 +}; diff --git a/merge-two-sorted-lists/hoyeongkwak.ts b/merge-two-sorted-lists/hoyeongkwak.ts new file mode 100644 index 000000000..6d07aa11b --- /dev/null +++ b/merge-two-sorted-lists/hoyeongkwak.ts @@ -0,0 +1,30 @@ +/** + * 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(m + n) + space complexity : O(m + n) +*/ + +function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { + if (list1 == null) return list2 + if (list2 == null) return list1 + + if (list1.val < list2.val) { + list1.next = mergeTwoLists(list1.next, list2) + return list1 + } else { + list2.next = mergeTwoLists(list1, list2.next) + return list2 + } + +}; diff --git a/word-search/hoyeongkwak.ts b/word-search/hoyeongkwak.ts new file mode 100644 index 000000000..0807d2813 --- /dev/null +++ b/word-search/hoyeongkwak.ts @@ -0,0 +1,40 @@ +function exist(board: string[][], word: string): boolean { + /* + O(m * n) + O(m * n) + */ + const rowsLen = board.length + const colsLen = board[0].length + const direction = [[0, 1], [1, 0], [0, -1], [-1, 0]] + const visited = Array(rowsLen).fill(0).map(() => Array(colsLen).fill(false)) + const dfs = (row: number, col: number, idx: number): boolean => { + if (idx === word.length) + return true + if (row < 0 || row >= rowsLen || col < 0 || + col >= colsLen || board[row][col] !== word[idx] || visited[row][col]) + return false + visited[row][col] = true + if (idx === word.length - 1) return true + for (const [dx, dy] of direction) { + const newRow = row + dx + const newCol = col + dy + + if (newRow >= 0 && newRow < rowsLen && newCol >= 0 && newCol < colsLen && !visited[newRow][newCol] && board[newRow][newCol] === word[idx + 1]) { + if (dfs(newRow, newCol, idx + 1)) { + return true + } + } + } + visited[row][col] = false + return false + } + + for (let r = 0; r < rowsLen; r++) { + for(let c = 0; c < colsLen; c++) { + if (board[r][c] === word[0] && dfs(r, c, 0)) { + return true + } + } + } + return false +};