Conversation
tokuhirat
reviewed
Jul 17, 2025
| @@ -0,0 +1,9 @@ | |||
| class Solution: | |||
| def uniquePaths(self, m: int, n: int) -> int: | |||
| rows = m | |||
There was a problem hiding this comment.
TORUS0818/leetcode#42 (comment)
実際には引数の変数名を変えることも考えたいですね。
あとは num_ をつけると実体ではなく個数であることが明確になる気がします。とはいえ10行以下のコードなのでrowsでも問題ないとは思います。
Owner
Author
There was a problem hiding this comment.
参照リンク、考えを整理するのに参考になりました。ありがとうございます。
そうですね、私も実務なら引数自体を変えているのかなと思います。もちろんリンクの通りすでに使っている場所があるかは考える必要がありますが…。
Owner
Author
There was a problem hiding this comment.
あと rows vs. num_rows は私も少し悩みました。無闇に省略しているわけではないし、おっしゃるとおり短いコードなので、短く書ける方を優先した形です。
tokuhirat
reviewed
Jul 17, 2025
| for position in range(1, width): | ||
| # 2d DP でいうと | ||
| # paths[position - 1]: 前の列, paths[position]: 前の行 | ||
| paths[position] = paths[position] + paths[position - 1] |
There was a problem hiding this comment.
paths[position] += paths[position - 1] もありでしょうか。
tokuhirat
reviewed
Jul 17, 2025
| for col in range(1, cols): | ||
| print(col) | ||
| paths[col] = previous_paths[col] + paths[col - 1] | ||
| previous_paths, paths = paths, [1] * cols |
There was a problem hiding this comment.
swap が必要なら(やや見にくいながら)この書き方もありですが、今回は
previous_paths = paths
paths = [1] * cols
で良いと思いました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
62. Unique Paths
https://leetcode.com/problems/unique-paths/