Open
Conversation
mamo3gr
reviewed
Mar 19, 2026
| - 長さが2の場合 | ||
| - 0番目の金額と1番目の金額の大きい方 | ||
| - 長さが3の場合 | ||
| - 0番目の金額と3番目の金額の和と1番目の金額の大きい方 |
There was a problem hiding this comment.
Suggested change
| - 0番目の金額と3番目の金額の和と1番目の金額の大きい方 | |
| - 0番目の金額と2番目の金額の和と1番目の金額の大きい方 |
mamo3gr
reviewed
Mar 19, 2026
| if len(nums) == 1: | ||
| return nums[0] | ||
|
|
||
| max_robbed_amounts = [0] * len(nums) |
mamo3gr
reviewed
Mar 19, 2026
|
|
||
| ## Step2 | ||
|
|
||
| 配列を利用する方が読みやすいと思った。 |
There was a problem hiding this comment.
この感覚の言語化はいいと思いました。ちなみに私もこれに同意です(漸化式に対応してすんなり入ってくる)。メモリ使用量が許すならこちらを書きたいです。
mamo3gr
reviewed
Mar 19, 2026
| max_robbed_two_houses_ago = 0 # 2つ前の家までで得られた最大金額 | ||
| max_robbed_one_house_ago = 0 # 1つ前の家までで得られた最大金額 | ||
| for n in nums: | ||
| amount = max(max_robbed_one_house_ago, max_robbed_two_houses_ago + n) |
There was a problem hiding this comment.
個人的には robbed_last, skipped_last が好みです。直前をスキップしている(盗んでいない)のだから、n が盗めるでしょう、という理屈が分かりやすいからです。
Suggested change
| amount = max(max_robbed_one_house_ago, max_robbed_two_houses_ago + n) | |
| amount = max(robbed_last, skipped_last + n) |
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
何の値かと、処理の文脈がわかりやすいと思いました。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
今回の問題
House Robber - LeetCode
使用言語
Python