Skip to content

Commit

Permalink
Merge pull request #539 from kjb512/main
Browse files Browse the repository at this point in the history
[kayden] Week 10 Solutions
  • Loading branch information
kjb512 authored Oct 19, 2024
2 parents fcd0185 + bacf0cc commit bd5bb4f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
33 changes: 33 additions & 0 deletions course-schedule/kayden.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions invert-binary-tree/kayden.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions jump-game/kayden.py
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions merge-k-sorted-lists/kayden.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions search-in-rotated-sorted-array/kayden.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bd5bb4f

Please sign in to comment.