-
-
Notifications
You must be signed in to change notification settings - Fork 335
[jamiebase] WEEK 14 Solutions #2632
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
jamiebase
wants to merge
8
commits into
DaleStudy:main
Choose a base branch
from
jamiebase: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.
+60
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab6622c
Add solution for checking if two binary trees are the same
jamiebase 606e0eb
Merge branch 'DaleStudy:main' into main
jamiebase 15012d8
Merge branch 'DaleStudy:main' into main
jamiebase 555369f
add: implement lowest common ancestor for binary search tree
jamiebase 1630474
Merge branch 'DaleStudy:main' into main
jamiebase 43860f8
Implement solution for House Robber II problem
jamiebase acebabb
Merge branch 'DaleStudy:main' into main
jamiebase 9e6973a
Implement solution for Subtree of Another Tree problem
jamiebase 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,32 @@ | ||
| """ | ||
| # Intuition | ||
| 집이 원형으로 놓여있기 때문에 첫 번째 집을 터는 경우, 안 터는 경우일 때로 나누어 해를 구하고, | ||
| 두 경우에서 최댓값 해를 구한다. | ||
|
|
||
| # Complexity | ||
| n은 nums의 길이라고 할 때, | ||
| - Time complexity: O(N) | ||
| - Space complexity: O(N) | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def rob(self, nums: list[int]) -> int: | ||
| n = len(nums) | ||
| if n == 1: | ||
| return nums[0] | ||
|
|
||
| # 첫 번째 집을 터는 경우 | ||
| dp1 = [0] * n | ||
| dp1[0] = dp1[1] = nums[0] | ||
| for i in range(2, n - 1): | ||
| dp1[i] = max(dp1[i - 2] + nums[i], dp1[i - 1]) | ||
|
|
||
| # 첫 번째 집을 안 터는 경우 | ||
| dp2 = [0] * n | ||
| dp2[1] = nums[1] | ||
| for j in range(2, n): | ||
| dp2[j] = max(dp2[j - 2] + nums[j], dp2[j - 1]) | ||
|
|
||
| return max(max(dp1), max(dp2)) | ||
|
jamiebase marked this conversation as resolved.
|
||
|
jamiebase marked this conversation as resolved.
|
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,28 @@ | ||
| """ | ||
| # Approach | ||
| 매 노드마다 같은 Tree인지 비교합니다. | ||
|
jamiebase marked this conversation as resolved.
|
||
|
|
||
| # Complexity | ||
| - Time complexity: 양 트리의 노드를 하나씩 다 비교해야 하므로 최악의 경우 O(N*M) | ||
| - Space complexity: root 트리 높이가 H라고 할 때 O(H)만큼 재귀 스택이 쌓임 | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
|
jamiebase marked this conversation as resolved.
|
||
| def isSameTree(self, a, b): | ||
| if not a and not b: | ||
| return True | ||
| if not a or not b: | ||
| return False | ||
| if a.val != b.val: | ||
| return False | ||
| return self.isSameTree(a.left, b.left) and self.isSameTree(a.right, b.right) | ||
|
|
||
| def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: | ||
| if not root: | ||
| return False | ||
|
|
||
|
jamiebase marked this conversation as resolved.
|
||
| if self.isSameTree(root, subRoot): | ||
| return True | ||
|
|
||
| return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.