Skip to content

Instantly share code, notes, and snippets.

@movefast
Created September 4, 2014 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save movefast/2bf80131abf19db89db1 to your computer and use it in GitHub Desktop.
Save movefast/2bf80131abf19db89db1 to your computer and use it in GitHub Desktop.
public class Solution {
public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return 0;
int[][] res = new int[obstacleGrid.length][obstacleGrid[0].length];
for(int i = 0; i < obstacleGrid.length; i++) {
for(int j = 0; j < obstacleGrid[0].length;j++) {
if(obstacleGrid[i][j] == 1) res[i][j] = 0;
else if(i == 0 && j == 0) res[0][0] = 1;
else if (i == 0) res[i][j] = res[i][j-1];
else if (j == 0) res[i][j] = res[i-1][j];
else res[i][j] = res[i-1][j]+res[i][j-1];
}
}
return res[obstacleGrid.length-1][obstacleGrid[0].length-1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment