-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/213. House Robber II #36
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
rob_prev = rob | ||
rob = max(rob, rob_with_i) | ||
return rob | ||
return nums[0] if len(nums) == 1 else \ |
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.
ここを二行に分けるならシンプルに書いた方が見やすいと思います。
if len(nums) == 1
return nums[0]
return max(rob_range(0, len(nums)-1), rob_range(1, len(nums)))
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.
確かにそうですね。最初に一行で書いて、ちょっと長くなったから改行ということをしていましたがちゃんと見直すべきでした。
return rob | ||
return nums[0] if len(nums) == 1 else \ | ||
max(rob_range(0, len(nums)-1), rob_range(1, len(nums))) | ||
``` |
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.
変数名が分かりやすかったです。
def rob(self, nums: List[int]) -> int: | ||
if len(nums) <= 1: | ||
return nums[-1] | ||
robbed_with_first = [nums[0], nums[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.
最初の家で盗むのであれば、 2 軒目では盗めないため、 [nums[0], 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.
確かにそうですね。ここは意味にこだわらないと違和感がありますね。
cache = dict() | ||
def wrap(*args, **kwargs): | ||
if args | ||
``` |
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.
確かに少し変でした。修正しました。
def my_cache(func):
global cache
cache = dict()
def wrap(*args, **kwargs):
if args in cache:
return cache[args]
cache[args] = func(*args, **kwargs)
return cache[args]
return wrap
class Solution:
def rob(self, nums: List[int]) -> int:
@my_cache
def rob_range(start: int, end: int) -> int:
rob = 0
rob_prev = 0
for i in range(start, end):
rob_with_i = rob_prev + nums[i]
rob_prev = rob
rob = max(rob, rob_with_i)
return rob
return nums[0] if len(nums) == 1 else \
max(rob_range(0, len(nums)-1), rob_range(1, len(nums)))
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.
cache は global とするより wrap 関数内で nonlocal と書きたいです。my_cache より広いスコープである必要はないですよね。global とすると以下のようにおかしなことが起きます。
@my_cache
def f(a, b):
return a * b
@my_cache
def g(a, b):
return a + b
print(f(1, 2)) # 2
print(g(1, 2)) # 2
さらに、cache は dict で、ミュータブルなオブジェクトを変更しているだけなので nonlocal も不要と思います。(手元で検証する際に from functools import cache とすると名前が衝突してしまい動かなくなってしまいました。名前空間を汚すのは、global を使うデメリットの一つですね。import functools として functools.cache とするのが良いということでもあると思いますが。)
ちなみに、この解法の場合 rob_range は2回しか呼ばれず、cache は使われないですね。
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.
nonlocalとglobalの使い分けができていませんでした。ご指摘いただきありがとうございます。
rob = max(rob, rob_with_i) | ||
return rob | ||
return nums[0] if len(nums) == 1 else \ | ||
max(rob_range(0, len(nums)-1), rob_range(1, len(nums))) |
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.
-の前後はスペースを入れるのが一般的だと思います。
https://peps.python.org/pep-0008/#other-recommendations
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.
Pep8はどちらでも良いらしいです。
fuga-98/arai60#24 (comment)
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.
ありがとうございます。自分も演算子の周りをどうするかは基本各自判断と記憶していて、入れたり入れなかったりですね。入れる方が良いという考えも頂戴しておきます
if len(nums) <= 1: | ||
return 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.
numsに空配列が渡される場合も考慮すると,以下のような書き方もあります
if len(nums) <= 2:
return max(nums, default=0)
問題文:https://leetcode.com/problems/house-robber-ii/description/