-
Notifications
You must be signed in to change notification settings - Fork 0
Create 213. House Robber II.md #36
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
tokuhirat
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,95 @@ | ||
| # 213. House Robber II | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - 0番目と n-1 番目を考えるとややこしいので、場合分けして直線のケースで解けばよさそう。 | ||
| ```python | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) <= 1: | ||
| return nums[0] | ||
|
|
||
| def rob_linear(nums: List[int]) -> int: | ||
| max_gain_robbed = nums[0] | ||
| max_gain_skipped = 0 | ||
| for i in range(1, len(nums)): | ||
| prev_max_gain_robbed = max_gain_robbed | ||
| max_gain_robbed = max_gain_skipped + nums[i] | ||
| max_gain_skipped = max(max_gain_skipped, prev_max_gain_robbed) | ||
| return max(max_gain_skipped, max_gain_robbed) | ||
|
|
||
| return max(rob_linear(nums[:-1]), rob_linear(nums[1:])) | ||
| ``` | ||
| #### memo | ||
| 時間計算量: $O(n)$ | ||
| 空間計算量: $O(1)$ | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/olsen-blue/Arai60/pull/36/files | ||
| - 関数名の inline は関数のインライン化が想起されそう。in_line であればOKか。上のSTEP1で書いた rob_linear は微妙で rob_linearly の方がよさそう。 | ||
| - https://github.com/olsen-blue/Arai60/pull/36/files#r1974943323 | ||
| > begin <= x < end の区間とする | ||
| - なるほど。シンプルになってすごい。 | ||
|
|
||
| 再帰バージョンを書いてみる。区間の端のうち含まれる start を動かした方がわかりやすいと思いました。 | ||
| ```python | ||
| import functools | ||
|
|
||
|
|
||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) == 1: | ||
| return nums[0] | ||
|
|
||
| @functools.cache | ||
| def rob_from_index(start: int, stop: int) -> int: | ||
| length = stop - start | ||
| if length == 1: | ||
| return nums[start] | ||
| if length == 2: | ||
| return max(nums[start], nums[start + 1]) | ||
| return max( | ||
| rob_from_index(start + 2, stop) + nums[start], | ||
| rob_from_index(start + 1, stop), | ||
| ) | ||
|
|
||
| return max(rob_from_index(0, len(nums) - 1), rob_from_index(1, len(nums))) | ||
| ``` | ||
|
|
||
| - https://github.com/hayashi-ay/leetcode/pull/50/files | ||
| - 関数内関数を書かないと微妙に異なる処理を読み解かないといけないので読みにくさを感じた。 | ||
| - 198\. House Robber の内容ですが、nums[0] に関する処理もループに入れられる。 | ||
| ```python | ||
| def rob_linear(nums: List[int]) -> int: | ||
| max_gain_robbed = 0 | ||
| max_gain_skipped = 0 | ||
| for i in range(len(nums)): | ||
| prev_max_gain_robbed = max_gain_robbed | ||
| max_gain_robbed = max_gain_skipped + nums[i] | ||
| max_gain_skipped = max(max_gain_skipped, prev_max_gain_robbed) | ||
| return max(max_gain_skipped, max_gain_robbed) | ||
| ``` | ||
| - 関数内関数の引数は nums で良いか微妙。nums 自体を渡しているわけではないため。money_list のようにした方が良いかもしれない。 | ||
| - len(nums) == 0 の場合は IndexError でよいと思っています。奪う家がない状況で 0円得られますというのは違いそう。ValueError にした方が呼び出し元にとってはわかりやすいかもしれない。結局はこの関数の使用想定によって異なるがあまりイメージできなかった。 | ||
|
|
||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if len(nums) <= 1: | ||
| return nums[0] | ||
|
|
||
| def rob_linearly(nums: List[int]) -> int: | ||
| max_gain_robbed = 0 | ||
| max_gain_skipped = 0 | ||
| for i in range(len(nums)): | ||
| prev_max_gain_robbed = max_gain_robbed | ||
| max_gain_robbed = max_gain_skipped + nums[i] | ||
| max_gain_skipped = max(max_gain_skipped, prev_max_gain_robbed) | ||
| return max(max_gain_skipped, max_gain_robbed) | ||
|
|
||
| return max(rob_linearly(nums[:-1]), rob_linearly(nums[1:])) | ||
| ``` | ||
|
|
||
| 3分,3分,2分で3回Accept | ||
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.
自分もこの実装が、わかりやすくて良いかと思いました。
特に問題ないと思います。