Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions 63_unique_paths_ii/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii/description/)
33 changes: 33 additions & 0 deletions 63_unique_paths_ii/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Step 1

- ある地点$grid[i][j]$に辿り着けるルートの数は$grid[i - 1][j]$に辿り着くルートの数と$grid[i][j - 1]$に辿り着くルートの数の和である

```python
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
M = len(obstacleGrid)
N = len(obstacleGrid[0])

if obstacleGrid[M - 1][N - 1] == 1 or obstacleGrid[0][0] == 1:
return 0

prev_row = [0] * N
for i, cell in enumerate(obstacleGrid[0]):
if cell == 0:
prev_row[i] = 1
else:
break

for i in range(1, M):
new_row = [0] * N
new_row[0] = prev_row[0] if obstacleGrid[i][0] == 0 else 0
for j in range(1, N):
if obstacleGrid[i][j] == 0:
new_row[j] = new_row[j - 1] + prev_row[j]
prev_row = new_row
return prev_row[N - 1]
```

時間計算量: $O(m \times n)$

空間計算量: $O(n)$
28 changes: 28 additions & 0 deletions 63_unique_paths_ii/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Step 2

```python
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
number_of_rows = len(obstacleGrid)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
h-masder/Arai60#18 (comment)

number_of_columns = len(obstacleGrid[0])

row = [0] * number_of_columns
for i, cell in enumerate(obstacleGrid[0]):
if cell == 0:
row[i] = 1
else:
break

for r in range(1, number_of_rows):
row[0] = row[0] if obstacleGrid[r][0] == 0 else 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if obstacleGrid[r][0] == 1:
    row[0] = 0

のほうがシンプルだと思いました。

for c in range(1, number_of_columns):
if obstacleGrid[r][c] == 0:
row[c] += row[c - 1]
else:
row[c] = 0
return row[number_of_columns - 1]
```

時間計算量: $O(m \times n)$

空間計算量: $O(n)$
30 changes: 30 additions & 0 deletions 63_unique_paths_ii/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Step 3

```python
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
number_of_rows = len(obstacleGrid)
number_of_columns = len(obstacleGrid[0])

row = [0] * number_of_columns
for i, cell in enumerate(obstacleGrid[0]):
if cell == 0:
row[i] = 1
else:
break

for r in range(1, number_of_rows):
row[0] = row[0] if obstacleGrid[r][0] == 0 else 0
for c in range(1, number_of_columns):
if obstacleGrid[r][c] == 0:
row[c] += row[c - 1]
else:
row[c] = 0
return row[number_of_columns - 1]
```

1回目: 4分 43秒

2回目: 3分 18秒

3回目: 3分 29秒