-
Notifications
You must be signed in to change notification settings - Fork 0
213. House Robber II #34
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
seal-azarashi
wants to merge
4
commits into
main
Choose a base branch
from
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.
+122
−0
Open
Changes from all commits
Commits
Show all changes
4 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,122 @@ | ||
| # 213. House Robber II | ||
|
|
||
| LeetCode URL: https://leetcode.com/problems/house-robber-ii/description/ | ||
|
|
||
| この問題は Java で解いています。 | ||
| 各解法において、メソッドが属するクラスとして `Solution` を定義していますが、これは Java の言語仕様に従い、コードを実行可能にするために必要なものです。このクラス自体には特定の意味はなく、単にメソッドを組織化し、実行可能にするためのものです。 | ||
|
|
||
| ## Step 1 | ||
|
|
||
| House Robber の拡張版のような問題。今回は家が円状に並んでおり、最初と最後の家が隣接する。 | ||
| 隣接する最初と最後の家は同時に盗みに入れないため、配列から最初 or 最後の家を除いた計2つの配列を作り、それぞれで盗めるお金の最大値から、より大きいものが答えになる。 | ||
| (💭 ヘルパーメソッドの中身をどうするかについては [House Robber のプルリクエスト](https://github.com/seal-azarashi/leetcode/pull/33)で議論がされていますので、そちらも合わせてご覧いただけますと嬉しいです) | ||
|
|
||
| ```java | ||
| /** | ||
| * 時間計算量: O(n): | ||
| * - O(n): 配列の先頭から末尾の要素を除いた全ての要素を対象に robHelper() を実行 | ||
| * - O(n): 配列2つ目の要素から末尾まで全ての要素を対象に robHelper() を実行 | ||
| * 空間計算量: O(1): 各走査で必要な変数等定数量の追加空間 | ||
| */ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| if (nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
| if (nums.length == 1) { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| return Math.max( | ||
| robHelper(0, nums.length - 1, nums), | ||
| robHelper(1, nums.length, nums) | ||
| ); | ||
| } | ||
|
|
||
| private int robHelper(int start, int end, int[] nums) { | ||
| int twoBackMaxCount = 0, oneBackMaxCount = 0; | ||
| for (int i = start; i < end; i++) { | ||
| int maxCount = Math.max(twoBackMaxCount + nums[i], oneBackMaxCount); | ||
|
|
||
| twoBackMaxCount = oneBackMaxCount; | ||
| oneBackMaxCount = maxCount; | ||
| } | ||
| return oneBackMaxCount; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Step 2 | ||
|
|
||
| 他の方のプルリクエストに目を通したが、注目すべき事項はほぼ [House Robber の方](https://github.com/seal-azarashi/leetcode/pull/33)を解く際に把握出来ていたようなので、今回は割愛。 | ||
|
|
||
| ## Step 3 | ||
|
|
||
| ```java | ||
| /** | ||
| * 解いた時間: 約4分 | ||
| * 時間計算量: O(n): | ||
| * - O(n): 配列の先頭から末尾の要素を除いた全ての要素を対象に robHelper() を実行 | ||
| * - O(n): 配列2つ目の要素から末尾まで全ての要素を対象に robHelper() を実行 | ||
| * 空間計算量: O(1): 各走査で必要な変数等定数量の追加空間 | ||
| */ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| if (nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
| if (nums.length == 1) { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| return Math.max( | ||
| robHelper(0, nums.length - 1, nums), | ||
| robHelper(1, nums.length, nums) | ||
| ); | ||
| } | ||
|
|
||
| private int robHelper(int start, int end, int[] nums) { | ||
| int twoBackMaxAmount = 0, oneBackMaxAmount = 0; | ||
| for (int i = start; i < end; i++) { | ||
| int maxAmount = Math.max(twoBackMaxAmount + nums[i], oneBackMaxAmount); | ||
|
|
||
| twoBackMaxAmount = oneBackMaxAmount; | ||
| oneBackMaxAmount = maxAmount; | ||
| } | ||
| return oneBackMaxAmount; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Step 4 | ||
|
|
||
| [oda さんのレビュー](https://github.com/seal-azarashi/leetcode/pull/34#discussion_r1791578173)を踏まえ関数名を修正。 | ||
|
|
||
| ```java | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| if (nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
| if (nums.length == 1) { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| return Math.max( | ||
| maxRobbableAmountInRange(0, nums.length - 1, nums), | ||
| maxRobbableAmountInRange(1, nums.length, nums) | ||
| ); | ||
| } | ||
|
|
||
| private int maxRobbableAmountInRange(int start, int end, int[] nums) { | ||
| int twoBackMaxAmount = 0, oneBackMaxAmount = 0; | ||
| for (int i = start; i < end; i++) { | ||
| int maxAmount = Math.max(twoBackMaxAmount + nums[i], oneBackMaxAmount); | ||
|
|
||
| twoBackMaxAmount = oneBackMaxAmount; | ||
| oneBackMaxAmount = maxAmount; | ||
| } | ||
| return oneBackMaxAmount; | ||
| } | ||
| } | ||
| ``` | ||
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.
本当は、robHelper が名前だけで何が返ってくるか分かると一番いいです。が、難しいですね。
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.
@oda
返信が大変遅くなりました。
ちょっと考えてみたのですが、
maxRobbableAmountInRangeはいかがでしょうか?Robbable は聞き慣れない単語ですが、Reddit にこんな投稿があったので、通じないことはないのかなと思い入れてみました。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.
いいんじゃないでしょうか。
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.
ありがとうございます!