Skip to content

62. Unique Paths#31

Open
tom4649 wants to merge 2 commits intomainfrom
62.Unique-Paths
Open

62. Unique Paths#31
tom4649 wants to merge 2 commits intomainfrom
62.Unique-Paths

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 27, 2026

62/sol1.py Outdated
total *= i

for i in range(1, n):
total /= i
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
total /= i
total //= i

最後に int() を使用しなくても、整数型の(割り切れる)割り算だったら //= でいいかもしれません。

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.

その通りですね、採用します。


class Solution:
def uniquePaths(self, m: int, n: int) -> int:
@lru_cache(maxsize=None)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

メモリの使用量を明示・制限しないなら、functools.cache の方が意図が明確になるかもしれません。Least Recently Used Cache ではなく、全てを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.

他の人の真似をして自分で何も考えられていませんでした。こちらの方が良さそうですね。
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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

数学の階乗の形 ((m + n - 2)! / (m - 1)!) に対応させて

for i in range(m + n - 2, m - 1, -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.

たしかにその組み合わせの式が頭にある人に対してはこの書き方が良さそうですが、ここではこのままにしておきます。
そこまで考えてコードを書いているんだと勉強になりました。

62/memo.md Outdated
# 62. Unique Paths

- sol1.py
- 愚直に解いた。高校数学の問題として捉えコーディングの要素は考えていない
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

アルゴリズムの文脈で愚直というと、brute forceのことを指すことが多いと思います。ここでは違った意味で使っていそうですかね?

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.

愚直=brute forceと解釈されうるという意味ですね。ここでは「思いついたものをそのまま書いた」という意味でした。
次回以降言い方変えようと思います。

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