-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 없이 접근한 노드들의 카운트만으로 풀 수 있지 않을까 싶습니다 ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 놓친 부분에 대해서 알려주셔서 감사합니다!