-
-
Notifications
You must be signed in to change notification settings - Fork 361
[yuseok89] WEEK 11 Solutions #2848
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 엄청 깔끔하게 해결하셨네요, 잘 보고 갑니다!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석binary-tree-maximum-path-sum/yuseok89.py# TC: O(N)
# SC: O(H)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
ans = float('-inf')
def get_max(cur: Optional[TreeNode]) -> int:
nonlocal ans
if cur is None:
return 0
left_max = get_max(cur.left)
right_max = get_max(cur.right)
ans = max(ans, cur.val, cur.val + left_max, cur.val + right_max, cur.val +left_max + right_max)
return max(cur.val, cur.val + max(left_max, right_max))
get_max(root)
return ans
📊 시간/공간 복잡도 분석
피드백: 전위 탐색과 재귀를 이용해 자식 노드의 최대 증가 합을 합산하고, 현재 노드에서 형성 가능한 최대 경로를 갱신한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # TC: O(N) | ||
| # SC: O(H) | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def maxPathSum(self, root: Optional[TreeNode]) -> int: | ||
|
|
||
| ans = float('-inf') | ||
|
|
||
| def get_max(cur: Optional[TreeNode]) -> int: | ||
| nonlocal ans | ||
|
|
||
| if cur is None: | ||
| return 0 | ||
|
|
||
| left_max = get_max(cur.left) | ||
| right_max = get_max(cur.right) | ||
|
|
||
| ans = max(ans, cur.val, cur.val + left_max, cur.val + right_max, cur.val +left_max + right_max) | ||
|
|
||
| return max(cur.val, cur.val + max(left_max, right_max)) | ||
|
|
||
| get_max(root) | ||
|
|
||
| return ans | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석merge-intervals/yuseok89.py# TC: O(NlogN)
# SC: O(N)
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
ans = list()
intervals.sort(key=lambda x:x[0])
ans.append([intervals[0][0]])
end = intervals[0][1]
for idx in range(1, len(intervals)):
if intervals[idx][0] <= end:
end = max(end, intervals[idx][1])
else:
ans[-1].append(end)
ans.append([intervals[idx][0]])
end = intervals[idx][1]
ans[-1].append(end)
return ans
📊 시간/공간 복잡도 분석
피드백: 정렬과 선형 순회로 겹치는 구간들을 합친다. 시작점 정렬이 핵심이다. 개선 제안: 현재 구현은 기본적인 풀이로 적절하다. 필요 시 예외 처리나 비어있는 입력에 대한 방어 코드를 추가할 수 있다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 구현 상당히 깔끔하게 잘 하셨네요 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # TC: O(NlogN) | ||
| # SC: O(N) | ||
| class Solution: | ||
| def merge(self, intervals: List[List[int]]) -> List[List[int]]: | ||
|
|
||
| ans = list() | ||
|
|
||
| intervals.sort(key=lambda x:x[0]) | ||
|
|
||
| ans.append([intervals[0][0]]) | ||
|
|
||
| end = intervals[0][1] | ||
|
|
||
| for idx in range(1, len(intervals)): | ||
| if intervals[idx][0] <= end: | ||
| end = max(end, intervals[idx][1]) | ||
| else: | ||
| ans[-1].append(end) | ||
| ans.append([intervals[idx][0]]) | ||
| end = intervals[idx][1] | ||
|
|
||
| ans[-1].append(end) | ||
|
|
||
| return ans | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석missing-number/yuseok89.py# TC: O(N)
# SC: O(1)
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
total = n * (n + 1) // 2
for num in nums:
total -= num
return total
📊 시간/공간 복잡도 분석
피드백: 등차수열의 합 공식을 이용해 한 번의 순회로 누락 숫자를 구한다. 개선 제안: 현재 구현이 간단하고 효율적이다. 입력에 빈 배열 등의 특수 케이스를 가정하지 않는다면 추가 방어가 필요할 수 있다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def missingNumber(self, nums: List[int]) -> int: | ||
| n = len(nums) | ||
| total = n * (n + 1) // 2 | ||
|
|
||
| for num in nums: | ||
| total -= num | ||
|
|
||
| return total | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석reorder-list/yuseok89.py# TC: O(N)
# SC: O(1)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return
def rec(right: Optional[ListNode]) -> Optional[ListNode]:
if not right:
return head
left = rec(right.next)
if not left:
return None
if left == right or left.next == right:
right.next = None
return None
nxt_left = left.next
left.next = right
right.next = nxt_left
return nxt_left
rec(head)
📊 시간/공간 복잡도 분석
피드백: 전형적인 위상 정렬/데이터 구조 조합으로 재배치한다. 재귀와 포인터 조작으로 in-place를 달성한다. 개선 제안: 현재 구현은 재귀를 이용해 오른쪽 부분을 순회하며 재배치를 시도한다. 구현의 복잡도가 다소 높으므로 반복적 접근으로 리팩토링을 고려해볼 수 있다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와우 엄청 깔끔하게 잘 푸셨네요
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석reorder-list/yuseok89.py# TC: O(N)
# SC: O(N)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return
def rec(right: Optional[ListNode]) -> Optional[ListNode]:
if not right:
return head
left = rec(right.next)
if not left:
return None
if left == right or left.next == right:
right.next = None
return None
nxt_left = left.next
left.next = right
right.next = nxt_left
return nxt_left
rec(head)
📊 시간/공간 복잡도 분석
피드백: 재귀를 이용해 오른쪽 부분의 노드를 따라가며 왼쪽 부분과 연결을 구성한다. 재귀 깊이가 O(n)까지 증가하므로 공간 복잡도는 스택 깊이에 의해 결정된다. 개선 제안: 고려해볼 만한 대안: 재귀 대신 두 포인터(빠른/느린)로 중간을 분리하고, 뒤에서부터 연결하기보다 배열에 노드를 모아 역순으로 연결하는 방식으로 공간 사용을 줄일 수 있다. 또한 tail 포인터를 유지해 불필요한 노드 재할당 없이 O(1) 공간으로 구현 가능하다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # TC: O(N) | ||
| # SC: O(N) | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reorderList(self, head: Optional[ListNode]) -> None: | ||
| """ | ||
| Do not return anything, modify head in-place instead. | ||
| """ | ||
|
|
||
| if not head or not head.next: | ||
| return | ||
|
|
||
| def rec(right: Optional[ListNode]) -> Optional[ListNode]: | ||
| if not right: | ||
| return head | ||
|
|
||
| left = rec(right.next) | ||
|
|
||
| if not left: | ||
| return None | ||
|
|
||
| if left == right or left.next == right: | ||
| right.next = None | ||
| return None | ||
|
|
||
| nxt_left = left.next | ||
| left.next = right | ||
| right.next = nxt_left | ||
|
|
||
| return nxt_left | ||
|
|
||
| rec(head) | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
binary-tree-maximum-path-sum/yuseok89.py
📊 시간/공간 복잡도 분석
피드백: 각 노드에서 왼쪽/오른쪽 자식에서의 최대 증가분을 구하고, 이를 이용해 현재 노드를 포함한 경로의 최대 합과 리턴 값을 계산한다.
개선 제안: 현재 구현은 전형적인 DFS 방식으로 동작하며, 공간 복잡도는 재귀 호출 깊이에 따라 좌우 깊이의 합 중 최댓값인 h에 비례한다. 필요 시 순회 방식으로의 변환이나 tail recursion 최적화를 고려해도 된다.