diff --git a/course-schedule/kayden.py b/course-schedule/kayden.py new file mode 100644 index 000000000..4e01b18d5 --- /dev/null +++ b/course-schedule/kayden.py @@ -0,0 +1,33 @@ +from collections import deque +class Solution: + # 시간복잡도: O(numCourses + prerequisites의 길이) + # 공간복잡도: O(numCourses + prerequisites의 길이) + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + + reachable = [0 for _ in range(numCourses)] + graph = [[] for _ in range(numCourses)] + + for a, b in prerequisites: + reachable[a] += 1 + graph[b].append(a) + + q = deque() + visited = set() + for i in range(numCourses): + if reachable[i] == 0: + q.append(i) + visited.add(i) + + while q: + node = q.popleft() + + for next_node in graph[node]: + reachable[next_node] -= 1 + if next_node not in visited and reachable[next_node] == 0: + q.append(next_node) + visited.add(next_node) + + if len(visited) == numCourses: + return True + + return False diff --git a/invert-binary-tree/kayden.py b/invert-binary-tree/kayden.py new file mode 100644 index 000000000..1545dd5b0 --- /dev/null +++ b/invert-binary-tree/kayden.py @@ -0,0 +1,19 @@ +class Solution: + # 시간복잡도: O(N) + # 공간복잡도: O(1) + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + def invert(node): + if not node: + return + + r = invert(node.left) + l = invert(node.right) + + node.right = r + node.left = l + + return node + + invert(root) + + return root diff --git a/jump-game/kayden.py b/jump-game/kayden.py new file mode 100644 index 000000000..ce37772c9 --- /dev/null +++ b/jump-game/kayden.py @@ -0,0 +1,15 @@ +class Solution: + # 시간복잡도: O(N) + # 공간복잡도: O(1) + # 이 문제는 마지막 테케가 통과하지 않아서 답을 참고했습니다. + def canJump(self, nums: List[int]) -> bool: + + gas = 0 + for n in nums: + if gas < 0: + return False + elif n > gas: + gas = n + gas -= 1 + + return True diff --git a/merge-k-sorted-lists/kayden.py b/merge-k-sorted-lists/kayden.py new file mode 100644 index 000000000..229e5f9c7 --- /dev/null +++ b/merge-k-sorted-lists/kayden.py @@ -0,0 +1,41 @@ +class Solution: + # 시간복잡도: O(NlogK) N: 모든 리스트의 노드 수 합 K: 리스트의 개수 + # 공간복잡도: O(1) 기존 노드 재활용 + def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: + if not lists: return None + if len(lists) == 1: return lists[0] + + def merge(a, b): + res = ListNode() + cur = res + + while a and b: + if a.val > b.val: + cur.next = b + b = b.next + else: + cur.next = a + a = a.next + cur = cur.next + + if a: + cur.next = a + else: + cur.next = b + + return res.next + + def mergeK(lo, hi): + if lo == hi: + return lists[lo] + + if hi - lo == 1: + return merge(lists[lo], lists[hi]) + + mid = (lo + hi) // 2 + left = mergeK(lo, mid) + right = mergeK(mid+1, hi) + + return merge(left, right) + + return mergeK(0, len(lists)-1) diff --git a/search-in-rotated-sorted-array/kayden.py b/search-in-rotated-sorted-array/kayden.py new file mode 100644 index 000000000..c3a5c9796 --- /dev/null +++ b/search-in-rotated-sorted-array/kayden.py @@ -0,0 +1,22 @@ +class Solution: + # 시간복잡도: O(logN) + # 공간복잡도: O(1) + def search(self, nums: List[int], target: int) -> int: + n = len(nums) + st, en = 0, n-1 + while st <= en: + mid = (st+en)//2 + if nums[mid] == target: + return mid + elif nums[mid] >= nums[st]: + if nums[st] <= target <= nums[mid]: + en = mid - 1 + else: + st = mid + 1 + else: + if nums[mid] <= target <= nums[en]: + st = mid + 1 + else: + en = mid - 1 + + return -1