Recursive version, although this one gets TLE, but in case anyone is interested

  • 1
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
            if(obstacleGrid.empty())return 0;
               return recur(0, 0, obstacleGrid.size()-1, obstacleGrid[0].size()-1, obstacleGrid);
        int recur(int i, int j, int xdim, int ydim, vector<vector<int>>& grid){
            static long long count = 0;
            if((i==xdim) && (j==ydim)) {count++; return count;}
            if((j < ydim) && grid[i][j+1] == 0) recur(i, j + 1, xdim, ydim, grid); //east
            if((i < xdim) && grid[i+1][j]== 0) recur(i+1, j, xdim, ydim, grid); //south
            return count;

Log in to reply

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