public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
for (int r = m  1; r >= 0; r) {
for (int c = n  1; c >= 0; c) {
if (obstacleGrid[r][c] == 1) obstacleGrid[r][c] = 0;
else {
if (r == m  1 && c == n  1) obstacleGrid[r][c] = 1;
else if (r == m  1) obstacleGrid[r][c] = obstacleGrid[r][c + 1];
else if (c == n  1) obstacleGrid[r][c] = obstacleGrid[r + 1][c];
else obstacleGrid[r][c] = obstacleGrid[r][c + 1] + obstacleGrid[r + 1][c];
}
}
}
return obstacleGrid[0][0];
}
Bottom up iterative solution, O(mn), no extra space


Here's same idea in C++
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); for(int i= m1;i>=0;i) for(int j=n1;j>=0;j) { if(obstacleGrid[i][j] == 1) obstacleGrid[i][j] = 0; else { if(i == m1 && j == n1) obstacleGrid[i][j] = 1; else if(i == m1) obstacleGrid[i][j] = obstacleGrid[i][j+1]; else if(j == n1) obstacleGrid[i][j] = obstacleGrid[i+1][j]; else obstacleGrid[i][j] = obstacleGrid[i][j+1] + obstacleGrid[i+1][j]; } } return obstacleGrid[0][0]; }