-
-
Notifications
You must be signed in to change notification settings - Fork 361
[parkhojeong] WEEK 12 Solutions #2858
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
base: main
Are you sure you want to change the base?
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,22 @@ | ||
| class Solution: | ||
| def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
| edge_dic = {i: [i] for i in range(n)} | ||
|
|
||
| for edge in edges: | ||
| start, end = sorted(edge) | ||
| edge_dic[start].append(end) | ||
| edge_dic[end].append(start) | ||
|
|
||
| def traverse(idx: int): | ||
| while edge_dic[idx]: | ||
| end = edge_dic[idx].pop() | ||
| traverse(end) | ||
|
|
||
| cnt = 0 | ||
| for i in range(n): | ||
| if len(edge_dic[i]) > 0: | ||
| cnt += 1 | ||
| traverse(i) | ||
|
|
||
| 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/parkhojeong.py# 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]:
cnt = 0
cur = head
while cur:
cnt += 1
cur = cur.next
dummy = ListNode()
dummy.next = head
prev = dummy
cur = head
i = 0
while cur:
if cnt - n == i:
prev.next = cur.next
break
prev = cur
cur = cur.next
i += 1
return dummy.next
📊 시간/공간 복잡도 분석
피드백: 카운트를 이용해 두 번째 포인터를 조정하는 방식으로 추가 포인터를 사용하지 않고도 제거 가능하다. 개선 제안: 현재 구현이 적절해 보입니다.
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. 이거 Single pass로 해결하는게 진짜 괜찮던데 한번 시도 해 보시는것도 좋겠네요 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # 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]: | ||
| cnt = 0 | ||
| cur = head | ||
| while cur: | ||
| cnt += 1 | ||
| cur = cur.next | ||
|
|
||
| dummy = ListNode() | ||
| dummy.next = head | ||
| prev = dummy | ||
| cur = head | ||
| i = 0 | ||
| while cur: | ||
| if cnt - n == i: | ||
| prev.next = cur.next | ||
| break | ||
|
|
||
| prev = cur | ||
| cur = cur.next | ||
| i += 1 | ||
|
|
||
| return dummy.next |
|
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/parkhojeong.py# 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 is None and q is None:
return True
if (p is None and q is not None) or (p is not None and q is None):
return False
if (p.left or q.left) and not self.isSameTree(p.left, q.left):
return False
if (p.right or q.right) and not self.isSameTree(p.right, q.right):
return False
return p.val == q.val
📊 시간/공간 복잡도 분석
피드백: 각 재귀 호출이 트리의 같은 위치의 노드를 비교하며, 최악의 경우 트리 높이에 비례하는 공간이 필요하다. 개선 제안: 현재 구현이 적절해 보입니다.
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,22 @@ | ||
| # 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 is None and q is None: | ||
| return True | ||
|
|
||
| if (p is None and q is not None) or (p is not None and q is None): | ||
| return False | ||
|
|
||
| if (p.left or q.left) and not self.isSameTree(p.left, q.left): | ||
| return False | ||
|
|
||
| if (p.right or q.right) and not self.isSameTree(p.right, q.right): | ||
| return False | ||
|
|
||
| return p.val == q.val | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
number-of-connected-components-in-an-undirected-graph/parkhojeong.py
📊 시간/공간 복잡도 분석
피드백: 인접 리스트를 한 방향으로만 탐색하는 재귀가 없이 스택으로 구현되어 있고, 각 정점을 한 번씩 방문하므로 시간은 간선 수와 정점 수의 합에 비례한다.
개선 제안: 현재 구현이 적절해 보입니다.