-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 63. Unique Paths II #34
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
|
|
||
| ### 感想 | ||
| - h>0, w>0 に加算を分けるとかなりスッキリ変形できて良い | ||
| - 地味なところだが、 意図がない限りは height -> width の順で操作することが統一されてる方が適切だと思った |
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.
これしていないと書いている本人が一番混乱すると思いますね。
| class Solution: | ||
| def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: | ||
| OBSTACLE = 1 | ||
| if obstacleGrid[0][0] == OBSTACLE: |
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.
[0][0]にアクセスする前に一応チェックしてもいいかもしれませんね。問題文に制約があるにせよ。
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.
レビューありがとうございます。
空のGridの考慮は完全に抜けてました。
obstacleGrid is None の場合は弾いておくべきですね。
| - https://github.com/sakupan102/arai60-practice/pull/35/files | ||
|
|
||
| ### 感想 | ||
| - h>0, w>0 に加算を分けるとかなりスッキリ変形できて良い |
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[0][0] = 1 | ||
| for row in range(num_rows): | ||
| for col in range(num_cols): | ||
| if obstacleGrid[row][col] == OBSTACLE: |
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.
レビューありがとうございます。
おっしゃる通りですね。
自分はネストを減らす意識を持つべきかもしれないです。
| return 0 | ||
| num_rows = len(obstacleGrid) | ||
| num_cols = len(obstacleGrid[0]) | ||
| sum_paths = [[0] * num_cols for _ in range(num_rows)] |
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のほうが流れ的に自然でしょうか?
| height = len(obstacleGrid) | ||
| sum_paths = [[0] * width for _ in range(height)] | ||
| sum_paths[0][0] = 1 | ||
| for h in range(1, 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.
自分は (width, height) に対しては (x, y) という変数を使うことが多いです。チームの平均的な書き方に合わせることをお勧めいたします。
|
|
||
| width = len(obstacleGrid[0]) | ||
| height = len(obstacleGrid) | ||
| 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.
sum_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.
レビューありがとうございます。
ある地点の結果が上と左の結果の和であることを利用して、
ある列の結果を、次の列で再利用する形ですね。
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]
63. Unique Paths II