-
-
Notifications
You must be signed in to change notification settings - Fork 361
[okyungjin] WEEK 12 Solutions #2860
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. 🏷️ 알고리즘 패턴 분석non-overlapping-intervals/okyungjin.py"""
https://leetcode.com/problems/non-overlapping-intervals/
Time: O(N log N), intervals 정렬
Space: O(1)
"""
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# end 오름차순 intervals 정렬
intervals.sort(key=lambda x: x[1])
# remove count
count = 0
last_end = float('-inf')
for start, end in intervals:
if start >= last_end: # 구간 안 겹침
last_end = end
else: # 구간 겹침
count += 1
return count
📊 시간/공간 복잡도 분석
피드백: 종료점으로 정렬한 뒤 현재 구간과의 겹침 여부를 판단하고 겹치면 제거 카운트를 증가시킨다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """ | ||
| https://leetcode.com/problems/non-overlapping-intervals/ | ||
|
|
||
| Time: O(N log N), intervals 정렬 | ||
| Space: O(1) | ||
| """ | ||
| class Solution: | ||
| def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
| # end 오름차순 intervals 정렬 | ||
| intervals.sort(key=lambda x: x[1]) | ||
|
|
||
| # remove count | ||
| count = 0 | ||
| last_end = float('-inf') | ||
|
|
||
| for start, end in intervals: | ||
| if start >= last_end: # 구간 안 겹침 | ||
| last_end = end | ||
| else: # 구간 겹침 | ||
| count += 1 | ||
|
|
||
| return count |
|
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. 🏷️ 알고리즘 패턴 분석same-tree/okyungjin.py"""
https://leetcode.com/problems/same-tree/description/
N: min(p노드수, q노드수)
Time: O(N)
Space: O(N)
"""
# 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
queue = deque([(p, q)])
while queue:
node_p, node_q = queue.popleft()
if not node_p and not node_q:
continue
elif node_p and node_q:
if node_p.val == node_q.val:
queue.append((node_p.left, node_q.left))
queue.append((node_p.right, node_q.right))
else:
return False
else:
return False
return True
📊 시간/공간 복잡도 분석
피드백: 큐에 노드를 쌍으로 담아 동시 탐색하며 각 위치의 값과 구조를 검사한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """ | ||
| https://leetcode.com/problems/same-tree/description/ | ||
|
|
||
| N: min(p노드수, q노드수) | ||
| Time: O(N) | ||
| Space: O(N) | ||
| """ | ||
|
|
||
| # 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
| queue = deque([(p, q)]) | ||
|
|
||
| while queue: | ||
| node_p, node_q = queue.popleft() | ||
|
|
||
| if not node_p and not node_q: | ||
| continue | ||
|
|
||
| elif node_p and node_q: | ||
| if node_p.val == node_q.val: | ||
| queue.append((node_p.left, node_q.left)) | ||
| queue.append((node_p.right, node_q.right)) | ||
| else: | ||
| return False | ||
|
|
||
| else: | ||
| return False | ||
|
|
||
| return True |
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.
🏷️ 알고리즘 패턴 분석
non-overlapping-intervals/okyungjin.py
📊 시간/공간 복잡도 분석
피드백: 끝점을 기준으로 정렬한 뒤, 현재 구간의 시작이 last_end 이상이면 겹치지 않는 구간으로 간주하고, 그렇지 않으면 제거 카운트를 증가시키는 방식이다.
개선 제안: 현재 구현이 적절해 보입니다.