Skip to content

62. Unique Paths#26

Open
n6o wants to merge 1 commit intomainfrom
unique-paths
Open

62. Unique Paths#26
n6o wants to merge 1 commit intomainfrom
unique-paths

Conversation

@n6o
Copy link
Owner

@n6o n6o commented Mar 15, 2026

今回の問題

Unique Paths - LeetCode

使用言語

Python

Copy link

@huyfififi huyfififi left a comment

Choose a reason for hiding this comment

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

読みやすかったです! step 1 に挙げられている解法は、私は思いつかなかったのですが、読んでいて納得感がありました。

私だったらですが、メモリ使用量が許容範囲内であれば、2 次元配列DPの方が1次元配列DPよりもわかりやすいと思うので、2次元配列を持って読みやすさを優先させたいような気持ちがあります。

@n6o
Copy link
Owner Author

n6o commented Mar 16, 2026

ありがとうございます。
確かにリソースの制約が問題ないのであれば、理解しやすい/読みやすい実装を選択した方がよいですね。
まだ選択肢を挙げて比べることができていないので反省しました。

Copy link

@5ky7 5ky7 left a comment

Choose a reason for hiding this comment

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

全体的に読みやすかったです.

- 今回は m+n < 200 なので動きそう

```py
class Solution:
Copy link

Choose a reason for hiding this comment

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

自分が思いついたのは,例えばm = 5, n = 2の時には5/2, 4/1をかけていく方法だったので,階乗をDPで保持しといて計算する方法が新鮮で参考になりました.


path_counts = [1] * m

# 空間計算量を O(m) に抑えるため、1次元配列を再利用する。
Copy link

Choose a reason for hiding this comment

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

今回はシンプルな問題なので1次元配列を一つ用意するのが良いと思いますが,一つ前の行と今見ている行の2つを用意して

current_path_counts[row] += previous_path_counts[row - 1]

のように更新するのも手かもしれません.
空間計算量をO(m)に保ちながら,場合によっては可読性が上がるので.

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

配列をコピーしながら処理していくイメージを持ちました。
今度実装してみます。勉強になりました。

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