Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int uniquePathsWithObstacles(int[][] obstacleGrid) {
- //check if null or blocked on step 1
- if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0][0] == 1){
- return 0;
- }
- int height = obstacleGrid.length;
- int width = obstacleGrid[0].length;
- int[][] paths = new int[height][width];
- //set first row and col to 1 if no obstacle exists
- for(int i=0; i<height; i++){
- if(obstacleGrid[i][0] != 1){
- paths[i][0] = 1;
- }else{
- break;
- }
- }
- for(int j=0; j<width; j++){
- if(obstacleGrid[0][j] != 1){
- paths[0][j] = 1;
- }else{
- break;
- }
- }
- //for all other spaces on the grid
- //start iterating at 1 since row 0 / col 0 is filled in already
- for(int i = 1; i< height; i++){
- for(int j = 1; j< width; j++){
- if(obstacleGrid[i][j] != 1){
- paths[i][j] = paths[i][j-1] + paths[i-1][j];
- }
- }
- }
- return paths[height-1][width-1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement