Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Python3/63. Unique Paths II.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Step 1. Initial Solution

- 基本的には前問のUnique Pathsと同じ考え方
- 前の行を保持しておくことで縦方向に足していき、ループの中で横方向にも足していく
- 注意点としては
- 一行目から横方向に足していく必要があること
- 一列目はcontinueすること

```python
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
num_rows = len(obstacleGrid)
num_cols = len(obstacleGrid[0])
paths_to_row: list[int] = [1] + [0] * (num_cols-1)

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.

むしろ開けない方が良いように読めました。先に計算される部分をくっつけておくことで分かりやすくする意図なのかなと思っています

If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:

# Correct:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
# Wrong:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)

Choose a reason for hiding this comment

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

おっしゃる通りですね。pep8を誤読していました。
black というフォーマッタのルールではほぼ全ての演算子の前後にスペースを入れるようになっており、勘違いしていました。
https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-breaks-binary-operators

Copy link

@tokuhirat tokuhirat Jun 23, 2025

Choose a reason for hiding this comment

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

paths_to_row だけではどのような変数か読み取ることができませんでした。例えば col_to_num_paths はいかがでしょうか。

for row in range(num_rows):
for col in range(num_cols):
if obstacleGrid[row][col] == 1:
paths_to_row[col] = 0
continue
if col == 0:
continue
paths_to_row[col] += paths_to_row[col-1]
return paths_to_row[-1]

```

### Complexity Analysis

- 時間計算量:O(mn)
- 空間計算量:O(n)

## Step 2. Alternatives

- https://github.com/olsen-blue/Arai60/pull/34/files
- OBSTACLE = 1のように置いた方がマジックナンバー感はない
- pathsを二次元で書いている
- 直感的ではあるかもしれないが、不要なものを取っておくのは少し抵抗がある
- 異常入力のエラーハンドリング
- list[list[int]]であることは関数の引数で指定しているので、[[]]になっているケースだけ処理できれば良さそう
Copy link

Choose a reason for hiding this comment

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

[[]]になっているケースだけ

どこまで考慮するかは別として、 [] みたいな空リストや [[1], [1,2]] みたいに長さが揃ってないときとかはその型だけでは弾けてないですね

- `paths_to_row: list[int] = [1] + [0] * (num_cols-1)` の部分はエラーにならない
- どうやら後ろの整数が0以下の場合は空のリストを作るらしい
- https://github.com/python/cpython/blob/3.13/Objects/listobject.c#L800
- https://github.com/sakupan102/arai60-practice/pull/35/files#r1615766614
- リストの初期化はループの外の方が素直とのこと
- 同感だが、たまに自分もやりそうになるので気を付けたい
- https://github.com/sakupan102/arai60-practice/pull/35/files#diff-b6a3dbef984cd8394a3638997aca4fa3993fee36b17ae420ea28914e44b80104R44
- 0で埋めてから0,0を1で初期化している
- こっちの方が素直なのか?
- 自分の感覚ではどっちが良いか分からなくなってしまった
- 違う言語でも読みやすいと感じた
- https://github.com/goto-untrapped/Arai60/pull/33/files#diff-79a50499a4e42f2697b078173d5edee715dee94830786cb69ffc4310e112ee63

## Step 3. Final Solution

- 大枠の方針は同じままで少し変更を加えた程度

```python
OBSTACLE = 1

class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
num_rows = len(obstacleGrid)
num_cols = len(obstacleGrid[0])
paths_to_row = [1 if col == 0 else 0 for col in range(num_cols)]

Choose a reason for hiding this comment

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

paths_to_row = [0] * num_cols
paths_to_row[0] = 1

でよいと思いました。


for row in range(num_rows):
for col in range(num_cols):
if obstacleGrid[row][col] == OBSTACLE:
paths_to_row[col] = 0
continue
if col == 0:
continue
paths_to_row[col] += paths_to_row[col-1]

return paths_to_row[-1]
```