-
Notifications
You must be signed in to change notification settings - Fork 0
213. House Robber II #28
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
hiro111208
wants to merge
1
commit into
main
Choose a base branch
from
213-house-robber-ii
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
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 @@ | ||
| ## 問題: [213 House Robber II](https://leetcode.com/problems/house-robber-ii/description/) |
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,29 @@ | ||
| # Step 1 | ||
|
|
||
| - $i$番目まで訪れた時の最大利益$maxAmount[i]$は$max(maxAmount[i - 1], maxAmount[i - 2] + nums[i])$ | ||
| - 円形のため、$0\le i \le nums.size -2$の範囲の最大利益と$1\le i \le nums.size -1$の範囲の最大利益のうち大きい方を返す | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) <= 2: | ||
| return max(nums) | ||
| first_max_amount_prev_2 = nums[0] | ||
| first_max_amount_prev_1 = max(nums[0], nums[1]) | ||
| for i in range(2, len(nums) - 1): | ||
| current_max_amount = max(first_max_amount_prev_1, first_max_amount_prev_2 + nums[i]) | ||
| first_max_amount_prev_2 = first_max_amount_prev_1 | ||
| first_max_amount_prev_1 = current_max_amount | ||
|
|
||
| second_max_amount_prev_2 = nums[1] | ||
| second_max_amount_prev_1 = max(nums[1], nums[2]) | ||
| for i in range(3, len(nums)): | ||
| current_max_amount = max(second_max_amount_prev_1, second_max_amount_prev_2 + nums[i]) | ||
| second_max_amount_prev_2 = second_max_amount_prev_1 | ||
| second_max_amount_prev_1 = current_max_amount | ||
| return max(first_max_amount_prev_1, second_max_amount_prev_1) | ||
| ``` | ||
|
|
||
| 時間計算量: $O(n)$ | ||
|
|
||
| 空間計算量: $O(1)$ |
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,26 @@ | ||
| # Step 2 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) <= 2: | ||
| return max(nums) | ||
|
|
||
| def rob_linear(start, end): | ||
| profit_two_back = nums[start] | ||
| profit_one_back = max(nums[start], nums[start + 1]) | ||
|
|
||
| for i in range(start + 2, end): | ||
| current_profit = max(profit_one_back, profit_two_back + nums[i]) | ||
| profit_two_back = profit_one_back | ||
| profit_one_back = current_profit | ||
|
|
||
| return profit_one_back | ||
|
|
||
| return max(rob_linear(0, len(nums) - 1), rob_linear(1, len(nums))) | ||
|
|
||
| ``` | ||
|
|
||
| 時間計算量: $O(n)$ | ||
|
|
||
| 空間計算量: $O(1)$ | ||
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 @@ | ||
| # Step 3 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) <= 2: | ||
| return max(nums) | ||
|
|
||
| def rob_linear(start, end): | ||
| profit_two_back = nums[start] | ||
| profit_one_back = max(nums[start], nums[start + 1]) | ||
|
|
||
| for i in range(start + 2, end): | ||
| current_profit = max(profit_one_back, profit_two_back + nums[i]) | ||
| profit_two_back = profit_one_back | ||
| profit_one_back = current_profit | ||
|
|
||
| return profit_one_back | ||
|
|
||
| return max(rob_linear(0, len(nums) - 1), rob_linear(1, len(nums))) | ||
|
|
||
| ``` | ||
|
|
||
| 1回目: 2分 57秒 | ||
|
|
||
| 2回目: 2分 18秒 | ||
|
|
||
| 3回目: 2分 14秒 |
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.
いいと思います。