Conversation
62/sol1.py
Outdated
| total *= i | ||
|
|
||
| for i in range(1, n): | ||
| total /= i |
There was a problem hiding this comment.
| total /= i | |
| total //= i |
最後に int() を使用しなくても、整数型の(割り切れる)割り算だったら //= でいいかもしれません。
|
|
||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| @lru_cache(maxsize=None) |
There was a problem hiding this comment.
メモリの使用量を明示・制限しないなら、functools.cache の方が意図が明確になるかもしれません。Least Recently Used Cache ではなく、全てをcacheしていると思うので。
There was a problem hiding this comment.
他の人の真似をして自分で何も考えられていませんでした。こちらの方が良さそうですね。
https://docs.python.org/3/library/functools.html#functools.cache
| if m < n: | ||
| m, n = n, m | ||
|
|
||
| for i in range(m, m + n - 1): |
There was a problem hiding this comment.
数学の階乗の形 ((m + n - 2)! / (m - 1)!) に対応させて
for i in range(m + n - 2, m - 1, -1):のように書くと意図が受け取りやすいと感じましたが、私の好みの問題かもしれません。
There was a problem hiding this comment.
たしかにその組み合わせの式が頭にある人に対してはこの書き方が良さそうですが、ここではこのままにしておきます。
そこまで考えてコードを書いているんだと勉強になりました。
62/memo.md
Outdated
| # 62. Unique Paths | ||
|
|
||
| - sol1.py | ||
| - 愚直に解いた。高校数学の問題として捉えコーディングの要素は考えていない |
There was a problem hiding this comment.
アルゴリズムの文脈で愚直というと、brute forceのことを指すことが多いと思います。ここでは違った意味で使っていそうですかね?
There was a problem hiding this comment.
愚直=brute forceと解釈されうるという意味ですね。ここでは「思いついたものをそのまま書いた」という意味でした。
次回以降言い方変えようと思います。
https://leetcode.com/problems/unique-paths/description/