LeetCode 63. Unique Paths II
程序员文章站
2022-06-30 22:54:59
...
题目
思路
动归+滚动数组
代码
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m = len(obstacleGrid)
n = len(obstacleGrid[0])
if obstacleGrid[m - 1][n - 1]: return 0
dp = [1 if not obstacleGrid[0][i] else 0 for i in range(n)]
for i in range(1, len(dp)): dp[i] = 1 if dp[i - 1] and dp[i] else 0
for i in range(1, m):
if obstacleGrid[i][0]: dp[0] = 0
for j in range(1, n):
dp[j] = dp[j - 1] + dp[j] if not obstacleGrid[i][j] else 0
return dp[n - 1]
推荐阅读
-
LeetCode 63. 不同路径 II
-
[leetcode]63. 不同路径 II
-
LeetCode——63.不同路径 II
-
63. Unique Paths II 动态规划
-
63. Unique Paths II 动态规划
-
【leetcode】Unique Paths II(动态规划)
-
LeetCode 63. Unique Paths II(动态规划)
-
[LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
-
【LeetCode62 Unique Paths】动态规划计算路径
-
【动态规划】LeetCode 63. Unique Paths II