Skip to content

Conversation

Satorien
Copy link
Owner

rob_prev = rob
rob = max(rob, rob_with_i)
return rob
return nums[0] if len(nums) == 1 else \
Copy link

@fuga-98 fuga-98 Jul 30, 2025

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)))

Copy link
Owner Author

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)))
```
Copy link

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]]
Copy link

Choose a reason for hiding this comment

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

最初の家で盗むのであれば、 2 軒目では盗めないため、 [nums[0], 0] としたほうが意味的には正しいと思います。おそらく同じ出力になると思います。

Copy link
Owner Author

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
```

Choose a reason for hiding this comment

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

正しく理解されているか記載いただいた内容からは読み取れませんでした。一度実装してみることをおすすめいたします。

Copy link
Owner Author

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)))

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 は使われないですね。

Copy link
Owner Author

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)))

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

Copy link

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)

Choose a reason for hiding this comment

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

補足いただきありがとうございます。単に自分がよく見るコードがそうだっただけみたいですね。。。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。自分も演算子の周りをどうするかは基本各自判断と記憶していて、入れたり入れなかったりですね。入れる方が良いという考えも頂戴しておきます

Comment on lines +14 to +15
if len(nums) <= 1:
return nums[-1]

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)

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.

5 participants