Skip to content

198. House Robber#28

Open
n6o wants to merge 1 commit intomainfrom
house-robber
Open

198. House Robber#28
n6o wants to merge 1 commit intomainfrom
house-robber

Conversation

@n6o
Copy link
Owner

@n6o n6o commented Mar 18, 2026

今回の問題

House Robber - LeetCode

使用言語

Python

- 長さが2の場合
- 0番目の金額と1番目の金額の大きい方
- 長さが3の場合
- 0番目の金額と3番目の金額の和と1番目の金額の大きい方
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 0番目の金額と3番目の金額の和と1番目の金額の大きい方
- 0番目の金額と2番目の金額の和と1番目の金額の大きい方

if len(nums) == 1:
return nums[0]

max_robbed_amounts = [0] * len(nums)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_amounts でも通じそうですね。


## Step2

配列を利用する方が読みやすいと思った。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この感覚の言語化はいいと思いました。ちなみに私もこれに同意です(漸化式に対応してすんなり入ってくる)。メモリ使用量が許すならこちらを書きたいです。

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には 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)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
何の値かと、処理の文脈がわかりやすいと思いました。

Copy link

@5ky7 5ky7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

読みやすかったです!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants