Skip to content

Conversation

@Fuminiton
Copy link
Owner


### 感想
- h>0, w>0 に加算を分けるとかなりスッキリ変形できて良い
- 地味なところだが、 意図がない限りは height -> width の順で操作することが統一されてる方が適切だと思った
Copy link

Choose a reason for hiding this comment

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

これしていないと書いている本人が一番混乱すると思いますね。

class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
OBSTACLE = 1
if obstacleGrid[0][0] == OBSTACLE:
Copy link

Choose a reason for hiding this comment

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

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

レビューありがとうございます。
空のGridの考慮は完全に抜けてました。

obstacleGrid is None の場合は弾いておくべきですね。

- https://github.com/sakupan102/arai60-practice/pull/35/files

### 感想
- h>0, w>0 に加算を分けるとかなりスッキリ変形できて良い
Copy link

Choose a reason for hiding this comment

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

これ読みやすいですね

sum_paths[0][0] = 1
for row in range(num_rows):
for col in range(num_cols):
if obstacleGrid[row][col] == OBSTACLE:
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.

レビューありがとうございます。
おっしゃる通りですね。

自分はネストを減らす意識を持つべきかもしれないです。

return 0
num_rows = len(obstacleGrid)
num_cols = len(obstacleGrid[0])
sum_paths = [[0] * num_cols for _ in range(num_rows)]

Choose a reason for hiding this comment

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

num_pathsのほうが流れ的に自然でしょうか?

height = len(obstacleGrid)
sum_paths = [[0] * width for _ in range(height)]
sum_paths[0][0] = 1
for h in range(1, height):
Copy link

Choose a reason for hiding this comment

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

自分は (width, height) に対しては (x, y) という変数を使うことが多いです。チームの平均的な書き方に合わせることをお勧めいたします。


width = len(obstacleGrid[0])
height = len(obstacleGrid)
sum_paths = [[0] * width for _ in range(height)]
Copy link

Choose a reason for hiding this comment

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

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

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

ある地点の結果が上と左の結果の和であることを利用して、
ある列の結果を、次の列で再利用する形ですね。

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        OBSTACLE = 1
        if obstacleGrid is None:
            return -1
        if obstacleGrid[0][0] == OBSTACLE:
            return 0
        height = len(obstacleGrid)
        width = len(obstacleGrid[0])
        num_paths = [0] * width
        num_paths[0] = 1

        for y in range(height):
            for x in range(width):
                if obstacleGrid[y][x] == OBSTACLE:
                    num_paths[x] = 0
                    continue
                if x > 0:
                    num_paths[x] += num_paths[x - 1]
        return num_paths[width - 1]

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.

6 participants