-
-
Notifications
You must be signed in to change notification settings - Fork 305
[8804who] WEEK 11 solutions #2294
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
Open
8804who
wants to merge
5
commits into
DaleStudy:main
Choose a base branch
from
8804who:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # 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 __init__(self): | ||
| self.answer = -1e9 | ||
|
|
||
| def maxPathSum(self, root: Optional[TreeNode]) -> int: | ||
| self.getSum(root, 0) | ||
| return self.answer | ||
|
|
||
| def getSum(self, node, depth): | ||
| val = node.val | ||
| leftMax = self.getSum(node.left, depth+1) if node.left else 0 | ||
| rightMax = self.getSum(node.right, depth+1) if node.right else 0 | ||
|
|
||
| temp = val + (leftMax if leftMax > 0 else 0) + (rightMax if rightMax > 0 else 0) | ||
|
|
||
| if self.answer < temp: | ||
| self.answer = temp | ||
|
|
||
| if leftMax > rightMax: | ||
| val += leftMax if leftMax > 0 else 0 | ||
| else: | ||
| val += rightMax if rightMax > 0 else 0 | ||
|
|
||
| return val | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution: | ||
| def merge(self, intervals: List[List[int]]) -> List[List[int]]: | ||
| intervals.sort() | ||
|
|
||
| start = intervals[0][0] | ||
| end = intervals[0][1] | ||
| answer = [] | ||
| for interval in intervals[1:]+[[10001, 0]]: | ||
| if end < interval[0]: | ||
| answer.append([start, end]) | ||
| start = interval[0] | ||
| if interval[1] > end: | ||
| end = interval[1] | ||
| return answer | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class Solution: | ||
| def missingNumber(self, nums: List[int]) -> int: | ||
| return (len(nums)*(len(nums)+1)//2)-sum(nums) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from collections import deque | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def __init__(self): | ||
| self.q = deque() | ||
|
|
||
| def reorderList(self, head: Optional[ListNode]) -> None: | ||
| """ | ||
| Do not return anything, modify head in-place instead. | ||
| """ | ||
| temp = head | ||
|
|
||
| while temp: | ||
| self.q.append(temp.val) | ||
| temp = temp.next | ||
|
|
||
| self.makeList(head, 0) | ||
|
|
||
| def makeList(self, node, n): | ||
| if n == 0: | ||
| node.val = self.q.popleft() | ||
| else: | ||
| node.val = self.q.pop() | ||
| if self.q: | ||
| self.makeList(node.next, (n+1)%2) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
헉 queue 로 해결하는 방법 너무 신박하네요..! 👍
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.
이 방법이 신기해서 제미나이에게 조금 물어보니 파이썬의 경우에는 재귀 호출을 제한한다고 하네요 (신기한 사실..)
보통 1천번까지 제한하지만 옵션 설정으로 풀 수는 있나봐요 (하지만 프로세스 내 스택 메모리를 다쓰면 동일한 문제가 있다곤 하네요~)