-
-
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
7
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.
Open
Changes from all commits
Commits
Show all changes
7 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 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)) |
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.
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 경우 모두 DP 배열을 사용하여 각 집까지의 최대 도둑질 금액을 계산한다. 두 DP 배열을 각각 구한 후 최댓값을 선택하는 방식으로 원형 문제를 해결한다.
개선 제안: 현재 구현이 적절해 보입니다.