Skip to content

Conversation

@Fuminiton
Copy link
Owner

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]
Copy link

Choose a reason for hiding this comment

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

sum_paths[-1][-1]でもよいですね

Copy link
Owner Author

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]
```
Copy link

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)]
Copy link

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

Copy link
Owner Author

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)]

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はあまり意味が通らない気がします。

Copy link
Owner Author

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]

Choose a reason for hiding this comment

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

個人的にはwとhは領域に対して用いたいし、水平方向を向いている地図に対しては違和感があるのでrow、columnの方が好きです。

Copy link
Owner Author

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]
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.

コメントありがとうございます。
油断していました。


```python
class Solution:
def uniquePaths(self, height: int, width: int) -> int:

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)]

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]が一体何を示すのかを最初に書いてあげると予想して読みやすそうです

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。

「経路」を表すオブジェクトの配列を想像する

おっしゃる通りで、num_paths等にすべきでした。

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.

7 participants