-
-
Notifications
You must be signed in to change notification settings - Fork 361
[yuseok89] WEEK 12 Solutions #2855
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # TC: O(NlogN) | ||
| # SC: O(1) | ||
|
Member
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.
|
||
| class Solution: | ||
| def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
| end, cnt = float('-inf'), 0 | ||
| for s, e in sorted(intervals, key=lambda x: x[1]): | ||
| if s >= end: | ||
| end = e | ||
| else: | ||
| cnt += 1 | ||
| return cnt | ||
|
|
||
|
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. 🏷️ 알고리즘 패턴 분석remove-nth-node-from-end-of-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 removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
fwd, flw = head, head
for _ in range(n):
fwd = fwd.next
if not fwd:
return head.next
while fwd.next:
fwd = fwd.next
flw = flw.next
flw.next = flw.next.next
return head
📊 시간/공간 복잡도 분석
피드백: 전방 포인터를 n 만큼 먼저 이동시키고 뒤따르는 포인터를 함께 이동시켜 제거할 노드를 찾는다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # 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 removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
|
|
||
| fwd, flw = head, head | ||
|
Member
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.
|
||
|
|
||
| for _ in range(n): | ||
| fwd = fwd.next | ||
|
|
||
| if not fwd: | ||
| return head.next | ||
|
|
||
| while fwd.next: | ||
| fwd = fwd.next | ||
| flw = flw.next | ||
|
|
||
| flw.next = flw.next.next | ||
|
|
||
| return head | ||
|
|
||
|
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/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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p and q:
if p.val != q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
elif not p and not q:
return True
else:
return False
📊 시간/공간 복잡도 분석
피드백: 피크리컬 재귀로 모든 노드를 비교하며, 자식 비교를 동시 진행한다. 개선 제안: 현재 구현이 적절해 보입니다.
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,20 @@ | ||
| # 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
|
|
||
| if p and q: | ||
| if p.val != q.val: | ||
| return False | ||
| return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) | ||
| elif not p and not q: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
|
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. 🏷️ 알고리즘 패턴 분석serialize-and-deserialize-binary-tree/yuseok89.py# TC: O(N)
# SC: O(N)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
arr = []
q = deque()
q.append(root)
while q:
cur = q.popleft()
if cur:
arr.append(str(cur.val))
q.append(cur.left)
q.append(cur.right)
else:
arr.append('n')
return ','.join(arr)
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
values = data.split(',')
n = len(values)
if values[0] == 'n':
return None
root = TreeNode(int(values[0]))
q = deque()
q.append(root)
idx = 1
while idx < n:
par = q.popleft()
if values[idx] != 'n':
par.left = TreeNode(int(values[idx]))
q.append(par.left)
idx += 1
if idx < n and values[idx] != 'n':
par.right = TreeNode(int(values[idx]))
q.append(par.right)
idx += 1
return root
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))
📊 시간/공간 복잡도 분석
피드백: 전형적인 BFS 기반 직렬화 방식으로 모든 노드 정보를 보존한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # TC: O(N) | ||
| # SC: O(N) | ||
| # Definition for a binary tree node. | ||
| # class TreeNode(object): | ||
| # def __init__(self, x): | ||
| # self.val = x | ||
| # self.left = None | ||
| # self.right = None | ||
|
|
||
| class Codec: | ||
|
|
||
| def serialize(self, root): | ||
| """Encodes a tree to a single string. | ||
|
|
||
| :type root: TreeNode | ||
| :rtype: str | ||
| """ | ||
|
|
||
| arr = [] | ||
| q = deque() | ||
| q.append(root) | ||
|
|
||
| while q: | ||
|
|
||
| cur = q.popleft() | ||
|
|
||
| if cur: | ||
| arr.append(str(cur.val)) | ||
| q.append(cur.left) | ||
| q.append(cur.right) | ||
| else: | ||
| arr.append('n') | ||
|
|
||
| return ','.join(arr) | ||
|
|
||
|
|
||
| def deserialize(self, data): | ||
| """Decodes your encoded data to tree. | ||
|
|
||
| :type data: str | ||
| :rtype: TreeNode | ||
| """ | ||
|
|
||
| values = data.split(',') | ||
| n = len(values) | ||
|
|
||
| if values[0] == 'n': | ||
| return None | ||
|
|
||
| root = TreeNode(int(values[0])) | ||
| q = deque() | ||
| q.append(root) | ||
| idx = 1 | ||
|
|
||
| while idx < n: | ||
| par = q.popleft() | ||
|
|
||
| if values[idx] != 'n': | ||
| par.left = TreeNode(int(values[idx])) | ||
| q.append(par.left) | ||
| idx += 1 | ||
|
|
||
| if idx < n and values[idx] != 'n': | ||
| par.right = TreeNode(int(values[idx])) | ||
| q.append(par.right) | ||
| idx += 1 | ||
|
|
||
| return root | ||
|
|
||
| # Your Codec object will be instantiated and called as such: | ||
| # ser = Codec() | ||
| # deser = Codec() | ||
| # ans = deser.deserialize(ser.serialize(root)) | ||
|
|
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/yuseok89.py
📊 시간/공간 복잡도 분석
피드백: 끝 지점을 기준으로 방문하며 현재 선택된 마지막 끝점 end 와의 비교로 중복을 제거한다. 정렬이 주된 시간 복잡도이다.
개선 제안: 현재 구현이 적절해 보입니다.