Conversation
| if len(nums) < 2: | ||
| return max(nums) |
There was a problem hiding this comment.
制約からテストケースでは守られているのですが、len(nums)==0 だと ValueError が送出されますね。
There was a problem hiding this comment.
なるほど。実務を意識してそのような例外処理も加えようと思います。
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])) |
There was a problem hiding this comment.
はじめに max_nums を必要な長さだけ確保しておいて、max_nums[i] = ... と書く方が、漸化式(更新式)をイメージしやすくて良いと感じました。
There was a problem hiding this comment.
そのまま採用しました。Python以外の場合を考えてもその方が自然ですね。
| max_with_last = nums[0] | ||
| max_without_last = 0 |
There was a problem hiding this comment.
個人的には with/without よりも robbed/skipped がしっくりきました。
There was a problem hiding this comment.
今回はこのままにしておきます。
max_with_lastは必ずしも最後の要素を選択するわけではなく (max_with_last = max(max_with_last, max_without_last + nums[i])で前者が選ばれた場合)、robbed/skippedよりもwith/withoutの方がこの意味を残せると考えたためです。
There was a problem hiding this comment.
仰るとおり、robbed_last といいつつ直近の家を盗まない、ということがありますね。あんまり良くない命名でした。
ChatGPTと壁打ちした感じ、以下の選択肢もありそうでした。
max_through_last,max_through_second_lastwith(out)が「取る(盗む)」ニュアンスを生む可能性をさらに下げられそう
max_up_to_i_minus_1,max_up_to_minus_2- たぶん好みではないでしょうが
There was a problem hiding this comment.
フォローアップありがとうございます、参考にさせていただきます。
たしかに through だと「必ずしも選択しない」の意味は残せそうですね。
変数名はLLMが得意な領域だと思うので自分も迷ったら投げてみようと思います。
https://leetcode.com/problems/house-robber/