Skip to content

Create 198houserobber.md#35

Open
fuga-98 wants to merge 1 commit into
mainfrom
198houserobber
Open

Create 198houserobber.md#35
fuga-98 wants to merge 1 commit into
mainfrom
198houserobber

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

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

Comment thread 198houserobber.md

@cache
def get_max_money(self, nums: Tuple[int], index: int=-1) -> int:
if not nums:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

get_max_money関数の中でnumsの長さが変わることはないのでこのチェックはrob関数の中で行ってもよいと思いました。

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.

確かにありですね。
今回は別の関数に切り出したので、単体でも動くように値チェックをこちらでしてみました。

Comment thread 198houserobber.md

他で呼び出されても大丈夫なように値のチェックを多めに書いてみた。

indexは負でアクセスされても良いように、最大値を超えても良いようにしてみた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

負でアクセスできるようにするメリットがよくわからなかったのですが、どのようなケースを想定されていますか?
return self.get_max_money(tuple(nums), len(nums)-1) とすればインデックスを常に非負にすることができます。

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.

レビューありがとうございます。
この問題を解く上では必要ないのですが、
nums[-1]って書くように、indexを負で書いても動いたら便利かなと思って書きました。

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.

Goはindexが負だとだめなんですね、、、

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ん、nums[-1]は、意図せず末尾にアクセスしてしまうのでむしろアクセスできない方が良いのではと思ったのですが、どうなんでしょう...?(変なこと言ってたらすみません)

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.

すみません、言葉足らずでした。
この関数はnumsの0~indexの最大値を求める関数です。
配列で末尾にアクセスする方法としてnums[-1]と書くのと同様に、マイナスを使うことによって、末尾からindexを指定できても良いかもという気持ちで書きました。
なくてもよい機能ですし、再帰でやっているので負になるのは好ましくないかもなあという気持ちにもなりました。

Comment thread 198houserobber.md
return get_max_count(len(nums)-1)
```

これ、二回robがあったら壊れる?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/functools.html#functools.lru_cache
cache_clear というのがありますね。

Comment thread 198houserobber.md
def rob(self, nums: List[int]) -> int:
return self.get_max_money(tuple(nums))

@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.

メンバー関数としての cache だとインスタンスごとに過去の計算結果をすべて覚えていることになるかと思います。
同じインスタンスを再利用すると、だんだんメモリー使用量が増えていくのは好ましくないのではないでしょうか。

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.

レビューありがとうございます。
再利用した際のメモリ使用量は考慮できていませんでした。
関数内関数に置くか、maxsizeを指定するのが良いでしょうか。

Comment thread 198houserobber.md
Comment on lines +68 to +69
candidate = get_max_count(index-2) + nums[index]
return max(candidate, get_max_count(index-1))
Copy link
Copy Markdown

@olsen-blue olsen-blue Apr 17, 2025

Choose a reason for hiding this comment

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

個人的な感覚ですが、インデックスがiならi-1と詰めて書いてしまうのでもいいと感じますが、index-1とかって、どこに区切りがあるのかがぱっと見でよくわからないのでindex - 1にしたいかも、という気持ちになる気がします。

Comment thread 198houserobber.md
if num_houses == 1: # ここ忘れてWA
return nums[0]
max_money = [0] * num_houses
max_money[0] = nums[0]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

max_money[0]を0にして、max_money[1]をnums[0]にすれば、L27~28の場合分けが必要なくなりますね。

Comment thread 198houserobber.md
return nums[0]
if index == 1:
return max(nums[0], nums[1])
candidate = get_max_count(index-2) + nums[index]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には、これくらいならわざわざcandidateという変数におかずmaxの中に直接式を書いちゃいますかね(細かい好みですが)

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