-
-
Notifications
You must be signed in to change notification settings - Fork 304
[SAM] Week 7 solutions #128
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 4 commits
bb98228
16f7887
548c334
d551167
af9a5cf
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,24 @@ | ||
| # TC : O(n) | ||
| # SC : O(n) | ||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| q = collections.deque() | ||
| res = [] | ||
| q.append(root) | ||
|
|
||
| while q: | ||
| level = [] | ||
| qLen = len(q) | ||
|
|
||
| # traverse nodes in each level and append left and right subtree if node is None | ||
| for i in range(qLen): | ||
| node = q.popleft() | ||
| if node: | ||
| level.append(node.val) | ||
| # append the subtrees of q for the next level in advance | ||
| q.append(node.left) | ||
| q.append(node.right) | ||
| if level: | ||
| res.append(level) | ||
|
|
||
| return res |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # TC : O(log n) because its a balanced tree | ||
| # SC : O(1) | ||
| class Solution: | ||
| def lowestCommonAncestor( | ||
| self, root: "TreeNode", p: "TreeNode", q: "TreeNode" | ||
| ) -> "TreeNode": | ||
| current = root | ||
|
|
||
| # Return when they split to left and right | ||
| while current: | ||
| if p.val > current.val and q.val > current.val: | ||
| current = current.right | ||
| elif p.val < current.val and q.val < current.val: | ||
| current = current.left | ||
| else: | ||
| return current | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # TC : O(n) | ||
| # SC : O(1) | ||
| class Solution: | ||
| def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
| # Create a dummy node to handle edge cases such as removing the first node | ||
| dummy = ListNode(0, head) | ||
| left = dummy | ||
| right = head | ||
|
|
||
| # Move the right pointer n steps ahead | ||
| for _ in range(n): | ||
| right = right.next | ||
|
|
||
| # Move both pointers until right reaches the end | ||
| while right: | ||
| left = left.next | ||
| right = right.next | ||
|
|
||
| # Remove the nth node from the end | ||
| left.next = left.next.next | ||
|
|
||
| # Return the head of the modified list | ||
| 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. ๋ฐํฌ ์๋ฃ ๊ตฌ์กฐ๋ฅผ ์ฐ์ ์ ๊ทผ๋ ์ฌ๋ฐ๋ค์ : )
Contributor
Author
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. ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ ํจ์จ์ ์ผ๋ก ์ธ ์ ์๋๋ฐ ์๊ฐ์ด ๋๋ฉด ๋ค์ ์ง๋ณด๊ฒ ์ต๋๋ค ใ ใ
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. ์ ๋ฆฌ๋ทฐ๋ฅผ ๋นผ์๊ฒผ๋ค์ ๐คฃ
Contributor
Author
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. ์ฌ์ค deque๋ฅผ ์ ๋ชฐ๋์๋๋ฐ chatgpt์ stack์ ์๋ซ๋ถ๋ถ๊ณผ ์๋ถ๋ถ์ ๋์์ popํ ์ ์๋ ๋ฐฉ๋ฒ์ ๋ฌผ์ด๋ณด๋ deque๊ฐ ๊ทธ๋ฐ ์๋ฃ๊ตฌ์กฐ๋ผ๊ณ ํด์ ์ฒ์ ๋ฐฐ์ ์ต๋๋ค! ๊ทธ๋ฐ๋ฐ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ ์๋ ๊ฒ ๊ฐ๋ค์ฉ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # TC : O(n) | ||
| # SC : O(m) m being the sum of deque and dummy nodes | ||
| class Solution: | ||
| def reorderList(self, head: Optional[ListNode]) -> None: | ||
| if not head or not head.next or not head.next.next: | ||
| return | ||
|
|
||
| # Step 1: Use a deque to collect nodes | ||
| d = deque() | ||
| current = head | ||
| while current: | ||
| d.append(current) | ||
| current = current.next | ||
|
|
||
| # Step 2: Reorder the list using deque | ||
| dummy = ListNode(0) # Dummy node with value 0 | ||
| current = dummy | ||
| toggle = True | ||
|
|
||
| while d: | ||
| if toggle: | ||
| current.next = d.popleft() # Append from the front of the deque | ||
| else: | ||
| current.next = d.pop() # Append from the back of the deque | ||
| current = current.next | ||
| toggle = not toggle | ||
|
|
||
| # Step 3: Ensure the last node points to None | ||
| current.next = None | ||
|
|
||
| # Step 4: Reassign head to point to the new reordered list | ||
| head = dummy.next |
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.
์ด ๋ฌธ์ ๊ฐ ์ด๋ ๊ฒ ๊ฐ๋จํ ํ์ด๋ ์ ์๋ ๊ฑฐ์๊ตฐ์.. ๋ฐ๋ก ์ฝ๋ ์์ ํ๋ฌ ๊ฐ๋๋ค ๐จ