diff --git a/course-schedule/hoyeongkwak.ts b/course-schedule/hoyeongkwak.ts new file mode 100644 index 000000000..230ce0b04 --- /dev/null +++ b/course-schedule/hoyeongkwak.ts @@ -0,0 +1,25 @@ +function canFinish(numCourses: number, prerequisites: number[][]): boolean { + const graph: number[][] = Array.from({ length: numCourses }, () => []) + for (const [course, prereq] of prerequisites) { + graph[course].push(prereq) + } + const traversing = new Set() + const finished = new Set() + + const finish = (crs: number): boolean => { + if (traversing.has(crs)) return false + if (finished.has(crs)) return true + + traversing.add(crs) + for (const pre of graph[crs]) { + if (!finish(pre)) return false + } + traversing.delete(crs) + finished.add(crs) + return true + } + for (let crs = 0; crs < numCourses; crs++) { + if (!finish(crs)) return false + } + return true +}; diff --git a/invert-binary-tree/hoyeongkwak.ts b/invert-binary-tree/hoyeongkwak.ts new file mode 100644 index 000000000..b9a2738ad --- /dev/null +++ b/invert-binary-tree/hoyeongkwak.ts @@ -0,0 +1,24 @@ +/** + * 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 invertTree(root: TreeNode | null): TreeNode | null { + if (root == null) return null + const temp = root.left + root.left = root.right + root.right = temp + + invertTree(root.left) + invertTree(root.right) + return root +}; diff --git a/jump-game/hoyeongkwak.ts b/jump-game/hoyeongkwak.ts new file mode 100644 index 000000000..ba6dc8173 --- /dev/null +++ b/jump-game/hoyeongkwak.ts @@ -0,0 +1,19 @@ +function canJump(nums: number[]): boolean { + const memo: boolean[] = new Array(nums.length).fill(null) + const dfs = (start: number): boolean => { + if (start >= nums.length - 1) { + return true + } + if (memo[start] != null) return memo[start] + const maxJump = nums[start] + for (let i = 1; i <= maxJump; i++) { + if (dfs(start + i)) { + memo[start] = true + return true + } + } + memo[start] = false + return false + } + return dfs(0) +}; diff --git a/merge-k-sorted-lists/hoyeongkwak.ts b/merge-k-sorted-lists/hoyeongkwak.ts new file mode 100644 index 000000000..e48515ec3 --- /dev/null +++ b/merge-k-sorted-lists/hoyeongkwak.ts @@ -0,0 +1,27 @@ +/** + * 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 mergeKLists(lists: Array): ListNode | null { + const tempArray: Array = [] + lists.forEach((node) => { + while (node) { + tempArray.push(node) + node = node.next + } + }) + tempArray.sort((node1, node2) => node1.val - node2.val) + let result = tempArray[0] ?? null + tempArray.forEach((node, index, arr) => { + node.next = arr[index + 1] ?? null + }) + return result +}; diff --git a/search-in-rotated-sorted-array/hoyeongkwak.ts b/search-in-rotated-sorted-array/hoyeongkwak.ts new file mode 100644 index 000000000..af521100e --- /dev/null +++ b/search-in-rotated-sorted-array/hoyeongkwak.ts @@ -0,0 +1,27 @@ +function search(nums: number[], target: number): number { + // return nums.indexOf(target) + let left = 0 + let right = nums.length - 1 + + while (left <= right) { + const mid = Math.floor((left + right) / 2) + if (nums[mid] === target) { + return mid + } + + if (nums[left] <= nums[mid]) { + if (target >= nums[left] && target < nums[mid]) { + right = mid - 1 + } else { + left = mid + 1 + } + } else { + if (target > nums[mid] && target <= nums[right]) { + left = mid + 1 + } else { + right = mid - 1 + } + } + } + return -1 +};