Day 34 — Unique Paths II
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] pathCount = new int[m][n];
pathCount[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1;
for(int i = 1; i < n; i++) {
pathCount[0][i] =
obstacleGrid[0][i] == 0 ?
pathCount[0][i-1] : 0;
}
for(int i = 1; i < m; i++){
if(obstacleGrid[i][0] == 0) {
pathCount[i][0] = pathCount[i-1][0];
} else {
pathCount[i][0] = 0;
}
}
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
if(obstacleGrid[i][j] == 1) {
pathCount[i][j] =0;
continue;
}
if(obstacleGrid[i-1][j] == 0){
pathCount[i][j] += pathCount[i-1][j];
}
if(obstacleGrid[i][j-1] == 0){
pathCount[i][j] += pathCount[i][j-1];
}
}
}
return pathCount[m-1][n-1];
}
}