From e078a21713acb2699d6102e09fae780db1050a9b Mon Sep 17 00:00:00 2001 From: nittoco <166355467+nittoco@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:16:55 +0900 Subject: [PATCH] =?UTF-8?q?63.=20Unique=20Paths=20=E2=85=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/unique-paths-ii/description/ You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle. Return the number of possible unique paths that the robot can take to reach the bottom-right corner. The testcases are generated so that the answer will be less than or equal to 2 * 109. --- "63. Unique Paths \342\205\241" | 105 ++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 "63. Unique Paths \342\205\241" diff --git "a/63. Unique Paths \342\205\241" "b/63. Unique Paths \342\205\241" new file mode 100644 index 0000000..8667807 --- /dev/null +++ "b/63. Unique Paths \342\205\241" @@ -0,0 +1,105 @@ +### Step1 + +- 最初、row == 0 or column == 0の時1に全部してしまっていたが、それぞれ上、左に障害物があったら0になることに気づかず +- どのように初期化するか迷った末こうした + +```python + +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: + height = len(obstacleGrid) + width = len(obstacleGrid[0]) + num_paths = [[0] * width for _ in range(height)] + for row in range(height): + for column in range(width): + if obstacleGrid[row][column]: + continue + if row == 0 and column == 0: + num_paths[row][column] = 1 + continue + num_up = num_paths[row - 1][column] if row - 1 >= 0 else 0 + num_left = num_paths[row][column - 1] if column - 1 >= 0 else 0 + num_paths[row][column] = num_up + num_left + return num_paths[height - 1][width - 1] +``` + +## Step2 + +- 参考コード + - https://github.com/hayashi-ay/leetcode/pull/44/files + - https://github.com/shining-ai/leetcode/pull/34/files + - https://github.com/fhiyo/leetcode/pull/35/files +- 色々初期化ロジックはあるが、Step1の自分のやつが結構わかりやすいと思う(自画自賛) + - ただ[0][0]の処理は外でやった方が素直だったかも(https://github.com/goto-untrapped/Arai60/pull/33#discussion_r1665010694) +- 1DPでも書いてみる + - O(min(height, width))にするにはobstacles配列を行列転置する必要があり、ややこしいのでやめた + + ```python + + class Solution: + def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: + height = len(obstacleGrid) + width = len(obstacleGrid[0]) + num_paths = [0] * width + if obstacleGrid[0][0]: + return 0 + num_paths[0] = 1 + for row in range(height): + for column in range(width): + if obstacleGrid[row][column]: + num_paths[column] = 0 + continue + if column == 0: + continue + num_paths[column] += num_paths[column - 1] + return num_paths[-1] + ``` + +- でも[0][0]の処理を外でするとif row - 1≥0 else 0という書き方はできずif文のインデントが増えちゃうのか、難しい + +```python + +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: + if obstacleGrid[0][0]: + return 0 + width = len(obstacleGrid[0]) + height = len(obstacleGrid) + num_paths = [[0] * width for _ in range(height)] + num_paths[0][0] = 1 + height = len(num_paths) + width = len(num_paths[0]) + for row in range(height): + for column in range(width): + if obstacleGrid[row][column]: + continue + if column - 1 >= 0: + num_paths[row][column] += num_paths[row][column - 1] + if row - 1 >= 0: + num_paths[row][column] += num_paths[row - 1][column] + return num_paths[-1][-1] +``` + +## Step3 + +- 結局この書き方に + +```python +python +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: + height = len(obstacleGrid) + width = len(obstacleGrid[0]) + num_paths = [[0] * width for _ in range(height)] + for row in range(height): + for column in range(width): + if obstacleGrid[row][column]: + continue + if row == 0 and column == 0: + num_paths[row][column] = 1 + continue + up_num = num_paths[row - 1][column] if row - 1 >= 0 else 0 + left_num = num_paths[row][column - 1] if column - 1 >= 0 else 0 + num_paths[row][column] = up_num + left_num + return num_paths[-1][-1] +```