diff --git a/63_unique_paths_ii/problem.md b/63_unique_paths_ii/problem.md new file mode 100644 index 0000000..dbbf5d3 --- /dev/null +++ b/63_unique_paths_ii/problem.md @@ -0,0 +1 @@ +## 問題: [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii/description/) diff --git a/63_unique_paths_ii/step1.md b/63_unique_paths_ii/step1.md new file mode 100644 index 0000000..2de2c41 --- /dev/null +++ b/63_unique_paths_ii/step1.md @@ -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)$ diff --git a/63_unique_paths_ii/step2.md b/63_unique_paths_ii/step2.md new file mode 100644 index 0000000..a819928 --- /dev/null +++ b/63_unique_paths_ii/step2.md @@ -0,0 +1,28 @@ +# Step 2 + +```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] +``` + +時間計算量: $O(m \times n)$ + +空間計算量: $O(n)$ diff --git a/63_unique_paths_ii/step3.md b/63_unique_paths_ii/step3.md new file mode 100644 index 0000000..529751d --- /dev/null +++ b/63_unique_paths_ii/step3.md @@ -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秒