-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 62. Unique Paths #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| for h in range(1, height): | ||
| for w in range(1, width): | ||
| sum_paths[h][w] = sum_paths[h - 1][w] + sum_paths[h][w - 1] | ||
| return sum_paths[height - 1][width - 1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sum_paths[-1][-1]でもよいですね
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
これ気づきませんでした。短くていいですね。
| for w in range(1, width): | ||
| sum_paths[h][w] = sum_paths[h - 1][w] + sum_paths[h][w - 1] | ||
| return sum_paths[height - 1][width - 1] | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
読みやすいです
| ```python | ||
| class Solution: | ||
| def uniquePaths(self, num_row: int, num_col: int) -> int: | ||
| sum_paths = [[0] * num_col for _ in range(num_row)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
内側が掛け算で構わない、つまり、数字が同一オブジェクトであったとしても代入するときに immutable なので置き換えられるので構わないが、List は問題であるという認識をしておきましょう。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.chiagt4fb1lq
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
つまり、数字が同一オブジェクトであったとしても代入するときに immutable なので置き換えられるので構わないが、List は問題であるという認識をしておきましょう。
コメントありがとうございます。これ認識できていなかったです。。
[[0] * num_cols] * num_rowsとしてしまうと、
[0, 0, ..., 0] への参照がnum_rows回作られることになってしまうのですね。反省です。
>>> num_rows, num_cols = 3, 2
>>> grid = [[0] * num_cols] * num_rows
>>> print(grid)
[[0, 0], [0, 0], [0, 0]]
>>> grid[0][0] = 1
>>> print(grid)
[[1, 0], [1, 0], [1, 0]]| ```python | ||
| class Solution: | ||
| def uniquePaths(self, height: int, width: int) -> int: | ||
| sum_paths = [[0] * width for _ in range(height)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
僕はnum_pathsの方が好きです。sum_というのは(僕の観測範囲では)あまり見かけず、num_ならthe number of ...という意味なのだなと分かります。
the sum of pathsはあまり意味が通らない気がします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
おっしゃる通りnum_pathsの方が適切ですね。
| sum_paths[0][i] = 1 | ||
| for h in range(1, height): | ||
| for w in range(1, width): | ||
| sum_paths[h][w] = sum_paths[h - 1][w] + sum_paths[h][w - 1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的にはwとhは領域に対して用いたいし、水平方向を向いている地図に対しては違和感があるのでrow、columnの方が好きです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
領域に対して用いたい
これかなり納得できました。ありがとうございます。
| sum_paths = [1] * num_col | ||
| for _ in range(num_row - 1): | ||
| for c in range(1, num_col): | ||
| sum_paths[c] += sum_paths[c-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- の両側にスペースを空けることをお勧めいたします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントありがとうございます。
油断していました。
|
|
||
| ```python | ||
| class Solution: | ||
| def uniquePaths(self, height: int, width: int) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
height, width, h, w と似たindexを表す変数が違う意味をもってるので、他のコメントにもあるように命名を調整したほうがよさそうです〜
| ```python | ||
| class Solution: | ||
| def uniquePaths(self, height: int, width: int) -> int: | ||
| paths = [[0] * width for _ in range(height)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pathsという命名の配列を見ると、「経路」を表すオブジェクトの配列を想像するので実体と名前を合わせたほうが驚きが少なそうです。
num_of_pathsとかですかね。変数名が長くなることを嫌うなら、コメントでpaths[i][j]が一体何を示すのかを最初に書いてあげると予想して読みやすそうです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
「経路」を表すオブジェクトの配列を想像する
おっしゃる通りで、num_paths等にすべきでした。
62. Unique Paths