diff --git a/course-schedule/radiantchoi.py b/course-schedule/radiantchoi.py new file mode 100644 index 0000000000..9d3e967ae2 --- /dev/null +++ b/course-schedule/radiantchoi.py @@ -0,0 +1,25 @@ +from collections import deque, defaultdict + +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + posts = defaultdict(list) + indegree = [0] * numCourses + + for prerequisite in prerequisites: + posts[prerequisite[1]].append(prerequisite[0]) + indegree[prerequisite[0]] += 1 + + completed = 0 + q = deque(list(filter(lambda x: indegree[x] == 0, range(numCourses)))) + + while q: + current = q.popleft() + completed += 1 + + for post in posts[current]: + indegree[post] -= 1 + + if indegree[post] == 0: + q.append(post) + + return completed == numCourses diff --git a/invert-binary-tree/radiantchoi.ts b/invert-binary-tree/radiantchoi.ts new file mode 100644 index 0000000000..3c6a6f4999 --- /dev/null +++ b/invert-binary-tree/radiantchoi.ts @@ -0,0 +1,28 @@ +/** + * 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/radiantchoi.py b/jump-game/radiantchoi.py new file mode 100644 index 0000000000..6da01b5226 --- /dev/null +++ b/jump-game/radiantchoi.py @@ -0,0 +1,11 @@ +class Solution: + def canJump(self, nums: List[int]) -> bool: + reachable = [False] * len(nums) + destination = len(nums) - 1 + + for index, strength in list(enumerate(nums))[::-1]: + if index + strength >= destination: + reachable[index] = True + destination = index + + return reachable[0] diff --git a/search-in-rotated-sorted-array/radiantchoi.swift b/search-in-rotated-sorted-array/radiantchoi.swift new file mode 100644 index 0000000000..ac10532663 --- /dev/null +++ b/search-in-rotated-sorted-array/radiantchoi.swift @@ -0,0 +1,41 @@ +class Solution { + func search(_ nums: [Int], _ target: Int) -> Int { + var left = 0 + var right = nums.count - 1 + + while left <= right { + let mid = (left + right) / 2 + + // 일단, 찾았으면 바로 반환 + if nums[mid] == target { + return mid + } + + // 이 문제의 배열은 정렬되지 않은 것이 아니고, 정렬된 배열 두 개를 붙여 놨다고 생각하면 된다. + // 유효한 인덱스임이 보장된 left와 현재 인덱스인 mid를 비교한다. + if nums[left] <= nums[mid] { + // 이로써 left와 mid 사이가 이 "정렬된 부분"임을 체크한다. + // 정렬이 되어 있다면, 찾고자 하는 값이 이 "정렬된 범위" 내에 있는지 확인한다. + // left에서 >=를 이용한 이유는, left 자체가 정답 인덱스일 수도 있기 때문. + if target >= nums[left] && target < nums[mid] { + // 만약 그렇다면 통상적인 이진 탐색의 논리대로 가면 된다. + right = mid - 1 + } else { + // 만약 그렇지 않다면 원래 갔어야 할 방향의 반대 방향으로 가면 된다. + left = mid + 1 + } + } else { + // left와 mid 사이가 정렬이 틀어져 있다면, mid와 right 사이는 정렬되어 있는 것이다. + // 위와 동일하게 범위 확인을 하되, 방향을 반대로 가져가면 된다. + if target <= nums[right] && target > nums[mid] { + left = mid + 1 + } else { + right = mid - 1 + } + } + } + + // early return하지 못했다면, 배열 내에 찾고자 하는 수가 없는 것이다. + return -1 + } +}