-
Notifications
You must be signed in to change notification settings - Fork 0
198. House Robber #33
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 198. House Robber | ||
|
|
||
| - 動的計画法を書いた | ||
|
|
||
|
|
||
| https://github.com/naoto-iwase/leetcode/pull/40 | ||
| の実装2: without_lastとwith_lastの二変数だけで良い | ||
| - sol2.py | ||
|
|
||
| > このコードはスレッドセーフティーという意味でどうなっているでしょうか。 | ||
|
|
||
| - cacheを使う場合(メモ化再帰)、キャッシュはthread safe: multi-threadで使用できる | ||
|
|
||
| https://github.com/hroc135/leetcode/pull/33#discussion_r1899009212 | ||
| > フィボナッチになりそうですね。黄金比の n 乗なので 1.6^n くらいです | ||
| メモがない場合の計算量 | ||
| https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A3%E3%83%9C%E3%83%8A%E3%83%83%E3%83%81%E6%95%B0 | ||
|
|
||
|
|
||
| メモ化再帰も書いておく (sol3.py) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if not nums: | ||
| raise ValueError("The input list is empty") | ||
|
|
||
| if len(nums) < 2: | ||
| return max(nums) | ||
|
|
||
| max_nums = [nums[0], max(nums[0], nums[1])] + [0] * (len(nums) - 2) | ||
|
|
||
| for i in range(2, len(nums)): | ||
| max_nums[i] = max(max_nums[i - 1], max_nums[i - 2] + nums[i]) | ||
|
|
||
| return max_nums[-1] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if not nums: | ||
| raise ValueError("The input list is empty") | ||
|
|
||
| if len(nums) < 2: | ||
| return max(nums) | ||
|
|
||
| max_with_last = nums[0] | ||
| max_without_last = 0 | ||
|
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には with/without よりも robbed/skipped がしっくりきました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 仰るとおり、 ChatGPTと壁打ちした感じ、以下の選択肢もありそうでした。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. フォローアップありがとうございます、参考にさせていただきます。 |
||
|
|
||
| for i in range(1, len(nums)): | ||
| next_max_without_last = max_with_last | ||
| max_with_last = max(max_with_last, max_without_last + nums[i]) | ||
| max_without_last = next_max_without_last | ||
|
|
||
| return max_with_last | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from functools import cache | ||
|
|
||
|
|
||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| if not nums: | ||
| raise ValueError("The input list is empty") | ||
|
|
||
| @cache | ||
| def rob_helper(idx): | ||
| if idx < 2: | ||
| return max(nums[: idx + 1]) | ||
|
|
||
| return max(rob_helper(idx - 1), rob_helper(idx - 2) + nums[idx]) | ||
|
|
||
| return rob_helper(len(nums) - 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.
制約からテストケースでは守られているのですが、
len(nums)==0だとValueErrorが送出されますね。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.
なるほど。実務を意識してそのような例外処理も加えようと思います。