-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #539 from kjb512/main
[kayden] Week 10 Solutions
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |