Skip to content

Create 213. House Robber II.md#36

Open
fuga-98 wants to merge 1 commit into
mainfrom
213.-House-Robber-II
Open

Create 213. House Robber II.md#36
fuga-98 wants to merge 1 commit into
mainfrom
213.-House-Robber-II

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented Apr 20, 2025

Comment thread 213. House Robber II.md

class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 1:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

<= 3 でもいいですね。

Comment thread 213. House Robber II.md
cache = {}

def my_cache(f):
# global cache これはいらない
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python でレキシカルなスコープを作るためには、このように関数の中で関数を作って関数を返す方法を使います。

Comment thread 213. House Robber II.md
get_max_amount(1, len(nums)))
```

my_cacheは関数宣言の時に一度だけ走る。呼び出されたときはwrapperが動く。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

いちおう、確認です。このコードは wrapper と2度出ます。

def a(f):
    print("wrapper")
    c = {}
    def g(n):
        if n not in c:
            c[n] = f(n)
            print(f"{n} not in c")
        print(f"return c[{n}]")
        return c[n]
    return g

class A:
    def f(self):
        @a
        def g(n):
            print(f"g {n}")
            if n > 0:
                g(n - 2)
                g(n - 1)
        return g(5)

x = A()
x.f()
x.f()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

x.f()の時に、gが定義されるからですね。

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.

3 participants