Skip to content

198. House Robber#33

Open
tom4649 wants to merge 2 commits intomainfrom
198.House-Robber
Open

198. House Robber#33
tom4649 wants to merge 2 commits intomainfrom
198.House-Robber

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 28, 2026

Comment on lines +3 to +4
if len(nums) < 2:
return max(nums)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

制約からテストケースでは守られているのですが、len(nums)==0 だと ValueError が送出されますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど。実務を意識してそのような例外処理も加えようと思います。

198/sol1.py Outdated
max_nums = [nums[0], max(nums[0], nums[1])]

for i in range(2, len(nums)):
max_nums.append(max(max_nums[i - 2], max_nums[i - 1] + nums[i]))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

はじめに max_nums を必要な長さだけ確保しておいて、max_nums[i] = ... と書く方が、漸化式(更新式)をイメージしやすくて良いと感じました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

そのまま採用しました。Python以外の場合を考えてもその方が自然ですね。

Comment on lines +6 to +7
max_with_last = nums[0]
max_without_last = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には with/without よりも robbed/skipped がしっくりきました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

今回はこのままにしておきます。
max_with_lastは必ずしも最後の要素を選択するわけではなく (max_with_last = max(max_with_last, max_without_last + nums[i])で前者が選ばれた場合)、robbed/skippedよりもwith/withoutの方がこの意味を残せると考えたためです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

仰るとおり、robbed_last といいつつ直近の家を盗まない、ということがありますね。あんまり良くない命名でした。

ChatGPTと壁打ちした感じ、以下の選択肢もありそうでした。

  • max_through_last, max_through_second_last
    • with(out) が「取る(盗む)」ニュアンスを生む可能性をさらに下げられそう
  • max_up_to_i_minus_1, max_up_to_minus_2
    • たぶん好みではないでしょうが

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

フォローアップありがとうございます、参考にさせていただきます。
たしかに through だと「必ずしも選択しない」の意味は残せそうですね。
変数名はLLMが得意な領域だと思うので自分も迷ったら投げてみようと思います。

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.

2 participants