-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 198. House Robber #35
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
base: main
Are you sure you want to change the base?
Conversation
| ```python | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| SENTINEL = 0 |
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.
レビューありがとうございます。
私はこれを番兵とは呼ばないかなと思います。言葉としては柔らかく使われているのですが、多くの場合、範囲外へのアクセスや条件を吸収するために特殊な値を置いておくこと
言われてみれば、おっしゃる通りだなと納得しました。
今回であれば単に0で初期化で良いのですね。
| return max(nums) | ||
| prev_prev_max_money = nums[0] | ||
| prev_max_money = max(nums[0], nums[1]) | ||
| max_money = None |
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.
これは好みが分かれると思いますが、max_moneyの型がOptional[int]になってしまうので、0で初期化のほうがいいかなと思います。今回の場合はlen(nums) <= 2を処理しているので、下のforループを必ず通ることが保証されていますが、多分max_moneyの型Optional[int]とreturnの型のintで不整合という型エラーになるlinterが多いと思います。
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.
レビューありがとうございます。
確かに型を統一する意識は持たないといけませんね。気をつけます。
| ### 感想 | ||
| - 変数名が微妙、`max_money_so_far`あたりをつけるべき。 | ||
| - 更新のループは0からはじめるようにしても良いが、2から始めた方が意図が伝わる気がする。 | ||
| - メモ化再帰の発想がパッとは思い付かない。規則性がみえたら漸化式をおいてみる->ボトムアップのアプローチと考えがちな気がする。 |
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.
再帰を思いつく方法としては、
amount_total_money、prev_amount_total_money、prev_prev_amount_total_moneyを管理していけば良い。
という管理対象すべてtotal_moneyなので、つまり単純に前回の結果を使っている、みたいなところを意識すると比較的わかりやすい書き換えができると思います。
この場合、prev_prevが-2、prevが-1なので、この発想だとトップダウンになりやすい気がしました。
※ただし、キャッシュ量を制限しない単純なメモ化再帰だと過去のデータすべて保持してしまうので、メモリ使用量が増えるというのがあります。
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.
コメントありがとうございます。
慣れな気もするので、いただいたアドバイスを元に練習したいと思います。
| num + prev_prev_max_money, | ||
| prev_max_money | ||
| ) | ||
| prev_prev_max_money = prev_max_money |
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.
prev_prev_max_moneyが少し見にくいかなと感じました。
あまり良い案が思いつきませんが、prev2_max_moneyとかですかね。
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.
レビューありがとうございます。
前々回の〜〜といった日本語をよく見る気がしたので使いましたが、確かにprev_prev_みづらいですね。
"2つ前"であることを明示した書き方の方が良いですね。例えば以下など。
max_profit_two_houses_ago
max_profit_one_houses_ago
max_profitThere 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.
英語的に正しいかは分かりませんが、ニュアンスはそんな感じです。
198. House Robber