Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kayden] Week 10 Solutions #539

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the pairs prerequisites[i] are unique. 이 입력 조건 덕분에 방문한 노드를 재방문하는 케이스는 없을 것 같다고 생각됩니다
그래서 visited set 없이 접근한 노드들의 카운트만으로 풀 수 있지 않을까 싶습니다 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 놓친 부분에 대해서 알려주셔서 감사합니다!

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