Skip to content

62. Unique Paths#35

Open
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-62
Open

62. Unique Paths#35
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-62

Conversation

@skypenguins

Copy link
Copy Markdown
Owner

62. Unique Paths

次回予告: 63. Unique Paths II

@skypenguins skypenguins self-assigned this Jun 21, 2026
@skypenguins skypenguins changed the title add memo.md 62. Unique Paths Jun 21, 2026
Comment thread memo.md
if k == 0:
return 1
return k * factorial(k-1)
combination = factorial(m + n - 2) // (factorial(m-1) * factorial(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.

演算子周りの空白があるところとないところが混在しているので、統一したほうが良いと思います。

@skypenguins skypenguins Jun 23, 2026

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 memo.md
- DPではあるかなと思ったが、単純に組合せ数を求めるだけで良いと考えた
- 右に $n-1$ 回、下に $m-1$ 回だけ移動する
- 移動回数の合計 $(n-1)+(m-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.

実行時間を見積もってみてください。
重要なことは、どれくらいでプログラムが終了するかだと思います。

以下を読んでみてください。
https://nuc.hatenadiary.org/entry/2025/11/29/#%E7%A7%92%E3%81%A7%E3%81%AE%E5%88%A4%E6%96%AD
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.1itsm36fdjze

また、実行時間の見積もり方はいくつかあります。
1)公式ドキュメントに記載されている計算量をもとに実行時間を導く
2)Pythonなど実装を読んで計算量を求め、そこから実行時間を導く
3)実際に測ってみる

3つめの手段のは、計算量に関する手がかりがなかったり、実際に試してみないと分からなかったりする場合に使うと思います。それ以外のケースでは、基本的に上の二つの方法で見積もります。

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.

計算量はあくまで手段で、具体的な実行時間を見積もるべきというのは確かにそうですね。
今回の場合だと、入力の最大値が100で、 CPythonの実装で計測し math.comb(198, 99) は約 1.38 µs でした。

Comment thread memo.md
import math
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
return math.comb((m-1)+(n-1), m-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.

こちらのコメントをご参照ください。
mt2324/leetcode#2 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants