diff --git a/course-schedule/wogha95.js b/course-schedule/wogha95.js new file mode 100644 index 000000000..746bc08b5 --- /dev/null +++ b/course-schedule/wogha95.js @@ -0,0 +1,49 @@ +/** + * TC: O(V + E) + * SC: O(V + E) + * N: numCourses(all of vertex), P: prerequisites(all of edge) + */ + +/** + * @param {number} numCourses + * @param {number[][]} prerequisites + * @return {boolean} + */ +var canFinish = function (numCourses, prerequisites) { + const STEP = { + before: 0, + ing: 1, + after: 2, + }; + const stepBoard = Array.from({ length: numCourses }, () => STEP.before); + const board = Array.from({ length: numCourses }, () => []); + + for (const [a, b] of prerequisites) { + board[a].push(b); + } + + for (let index = 0; index < numCourses; index++) { + if (isCycle(index)) { + return false; + } + } + return true; + + function isCycle(current) { + if (stepBoard[current] === STEP.end) { + return false; + } + if (stepBoard[current] === STEP.ing) { + return true; + } + + stepBoard[current] = STEP.ing; + for (const next of board[current]) { + if (isCycle(next)) { + return true; + } + } + stepBoard[current] = STEP.end; + return false; + } +}; diff --git a/invert-binary-tree/wogha95.js b/invert-binary-tree/wogha95.js new file mode 100644 index 000000000..066c45ab5 --- /dev/null +++ b/invert-binary-tree/wogha95.js @@ -0,0 +1,33 @@ +/** + * 양쪽 자식 노드 주소를 교환하고 dfs로 순회합니다. + * + * TC: O(N) + * 모든 트리를 순회합니다. + * + * SC: O(N) + * 최악의 경우 (한쪽으로 치우친 트리) N만큼 CallStack이 생깁니다. + * + * N: tree의 모든 node 수 + */ + +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var invertTree = function (root) { + if (!root) { + return root; + } + [root.left, root.right] = [root.right, root.left]; + invertTree(root.left); + invertTree(root.right); + return root; +}; diff --git a/jump-game/wogha95.js b/jump-game/wogha95.js new file mode 100644 index 000000000..1d102141b --- /dev/null +++ b/jump-game/wogha95.js @@ -0,0 +1,37 @@ +/** + * TC: O(N) + * SC: O(1) + * N: nums.length + */ + +/** + * @param {number[]} nums + * @return {boolean} + */ +var canJump = function (nums) { + if (nums.length === 1) { + return true; + } + + let maximumIndex = 0; + + for (let index = 0; index < nums.length; index++) { + const jumpLength = nums[index]; + + if (jumpLength === 0) { + continue; + } + + if (maximumIndex < index) { + return false; + } + + maximumIndex = Math.max(maximumIndex, index + nums[index]); + + if (maximumIndex >= nums.length - 1) { + return true; + } + } + + return false; +}; diff --git a/search-in-rotated-sorted-array/wogha95.js b/search-in-rotated-sorted-array/wogha95.js new file mode 100644 index 000000000..4d8362737 --- /dev/null +++ b/search-in-rotated-sorted-array/wogha95.js @@ -0,0 +1,65 @@ +/** + * 모든 케이스가 많지 않다고 생각되어 분기처리하였습니다.. + * 더 간결한 풀이법을 고려해보는 중.. + * + * TC: O(log N) + * 이진탐색을 이용하여 순회합니다. + * + * SC: O(1) + * 이진탐색에 이용되는 투 포인터의 공간복잡도를 갖습니다. + */ + +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var search = function (nums, target) { + if (nums.length === 1) { + return target === nums[0] ? 0 : -1; + } + + let left = 0; + let right = nums.length - 1; + + while (left < right) { + const center = Math.floor((left + right) / 2); + if (target === nums[left]) { + return left; + } + if (target === nums[center]) { + return center; + } + if (target === nums[right]) { + return right; + } + + if (nums[left] <= nums[center] && nums[center] < nums[right]) { + if (target < nums[left] || nums[right] < target) { + return -1; + } else if (nums[left] < target && target < nums[center]) { + right = center; + } else if (nums[center] < target && target < nums[right]) { + left = center + 1; + } + } else if (nums[right] < nums[left] && nums[left] <= nums[center]) { + if (nums[right] < target && target < nums[left]) { + return -1; + } else if (nums[left] < target && target < nums[center]) { + right = center; + } else { + left = center + 1; + } + } else if (nums[center] < nums[right] && nums[right] < nums[left]) { + if (nums[center] < target && target < nums[right]) { + left = center + 1; + } else if (nums[right] < target && target < nums[left]) { + return -1; + } else { + right = center; + } + } + } + + return -1; +};