Skip to content

Instantly share code, notes, and snippets.

@zac-xin
Created November 5, 2012 16:44
Show Gist options
  • Save zac-xin/4018234 to your computer and use it in GitHub Desktop.
Save zac-xin/4018234 to your computer and use it in GitHub Desktop.
UniquePath2FromLeetcode
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int matrix[][] = new int[m][n];
for(int i = n - 1; i >= 0; i--){
if(obstacleGrid[m - 1][i] == 1)
break;
matrix[m - 1][i] = 1;
}
for(int i = m - 1; i >=0; i--){
if(obstacleGrid[i][n-1] == 1)
break;
matrix[i][n-1] = 1;
}
for(int i = m - 2; i >= 0; i--){
for(int j = n - 2; j >=0; j--){
if(obstacleGrid[i][j] == 1)
matrix[i][j] = 0;
else
matrix[i][j] = matrix[i+1][j] + matrix[i][j+1];
}
}
return matrix[0][0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment