7 lines java solution using DP


  • 0
    D
    public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
          int row=obstacleGrid.length,col=obstacleGrid[0].length;
          int[][] res=new int[row][col];
          res[0][0]=obstacleGrid[0][0]==1?0:1;
          for(int i=1;i<col;i++) res[0][i]=res[0][i]=obstacleGrid[0][i]==1?0:res[0][i-1];
          for(int i=1;i<row;i++) res[i][0]=res[i][0]=obstacleGrid[i][0]==1?0:res[i-1][0];
          for(int i=1;i<row;i++) for(int j=1;j<col;j++) res[i][j]=obstacleGrid[i][j]==1?0:res[i-1][j]+res[i][j-1];
          return res[row-1][col-1];
        }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.