Skip to content

LeetCode 62. Unique Paths#57

Open
huyfififi wants to merge 10 commits intomainfrom
leetcode-62-unique-paths
Open

LeetCode 62. Unique Paths#57
huyfififi wants to merge 10 commits intomainfrom
leetcode-62-unique-paths

Conversation

@huyfififi
Copy link
Owner

@huyfififi huyfififi self-assigned this Mar 10, 2026
count = [[0] * n for _ in range(m)]
for row in range(m):
for col in range(n):
if row == 0 or col == 0:
Copy link
Owner Author

Choose a reason for hiding this comment

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

読み直すと、上端か左端かの2つのチェックが一行にまとまっているのがちょっとわかりづらい?
step1.py のように初期化する方が素直かも。

Copy link

Choose a reason for hiding this comment

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

個人的にはこちらの方が読みやすいです.できるだけ同じ枠組みに入れてしまうのが好きと言うのもあるかもしれません.

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、他の方々との感覚のすり合わせができて助かります!

Copy link

Choose a reason for hiding this comment

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

僕は前者の方が目的が明確で読みやすく感じましたが、好みのレベルだと思います。どちらも読みやすいです!

Choose a reason for hiding this comment

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

こういう書き方もあるのかと勉強になりました 🙏

@huyfififi huyfififi marked this pull request as ready for review March 10, 2026 15:29
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` の grid を用意して埋めていけばいい。(`step1.py`)
時間計算量: O(mn), 空間計算量: O(mn)

`m * n` 全ての結果を持つ必要はなくて、次のrowを埋めるのに必要なのは直前のrowだけなので、2つのrowだけ持てばいい。(`step1_two_rows.py`)
Copy link

Choose a reason for hiding this comment

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

さらに言えば,rowの更新は左から右の一方向なので1つのrowを持てば十分ですね.つまりcurrent_row[col]を更新する直前,current_row[col]にはprev_row[col]と同じ値が入っているはずなので

current_row[col] += current_row[col - 1]

と更新できます.(可読性は直前と今で別変数に入れた方が良いでしょうが)



class Solution:
@functools.cache
Copy link

Choose a reason for hiding this comment

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

pythonにはこんなのがあるんですね.便利すぎてびっくりしました.

count = [[0] * n for _ in range(m)]
for row in range(m):
for col in range(n):
if row == 0 or col == 0:
Copy link

Choose a reason for hiding this comment

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

個人的にはこちらの方が読みやすいです.できるだけ同じ枠組みに入れてしまうのが好きと言うのもあるかもしれません.


[dxxsxsxkxさんのPR](https://github.com/dxxsxsxkx/leetcode/pull/33/changes#diff-e425ab53cee19df2ef6995889ba4e527fc0e29e480a6787c15119f2da066511c)

動的計画法だと初めからわかっていたのでそれに飛びついてしまったが、いつかの数学でやった組み合わせの問題であることも連想できた方が良かったな。
Copy link

Choose a reason for hiding this comment

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

これは僕も同じことしました。
こういうやり方もあるよくらいは言えた方が良さそうですよね。

count = [[0] * n for _ in range(m)]
for row in range(m):
for col in range(n):
if row == 0 or col == 0:
Copy link

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.

4 participants