/**
* similar with unique path problem, scan the matrix row by row
* if obstacleGrid[i][j] == 1, then dp[j] = 0
* otherwise, just use same method as unique path which sums dp[j-1] and dp[j] then save it to dp[j]
*/
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int n = obstacleGrid[0].length; // # of columns
int[] dp = new int[n];
dp[0] = 1;
for (int[] row : obstacleGrid)
for (int j=0; j<n; j++) {
if (row[j] == 1) dp[j] = 0;
else if (j > 0) dp[j] += dp[j-1];
}
return dp[n-1];
}
}
Leetcode 63. Unique Paths II
最新推荐文章于 2022-12-05 22:50:02 发布