LC 63 Unique Paths II(M)
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length<1)
return 0;
int width = obstacleGrid[0].length;
int[] dp= new int[width];
dp[0] = 1; //initial first value
for(int row = 0; row< obstacleGrid.length; row++){//process row
for(int col = 0; col < width; col++){
if(obstacleGrid[row][col]==1){
dp[col] = 0;
}else{
if(col > 0){
dp[col] = dp[col] + dp[col -1]; // Translate to dp[oldTop]+ dp[curLeft];
}
}
}
}
return dp[width - 1];
}Last updated