# Leetcode Unique Paths II problem solution

In this Leetcode Unique Paths II problem solution, A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and space are marked as 1 and 0 respectively in the grid.

## Problem solution in Python.

```class Solution:
def uniquePathsWithObstacles(self, og: List[List[int]]) -> int:
if og==1:
return 0
else:
og=1
for i in range(1,len(og)):
if og[i]!=1:
og[i]=og[i-1]
else:
og[i]=0
for i in range(1,len(og)):
if og[i]!=1:
og[i]=og[i-1]
else:
og[i]=0
for i in range(1,len(og)):
for j in range(1,len(og)):
if og[i][j]!=1:
og[i][j]=og[i-1][j]+og[i][j-1]
else:
og[i][j]=0
return og[-1][-1]
```

## Problem solution in Java.

```public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid.length;
int[][] s = new int[m][n];
s = obstacleGrid==0 ? 1:0;
if(s == 0) return 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(obstacleGrid[i][j] == 1) s[i][j] = 0;
else if(i==0){
if(j>0) s[i][j] = s[i][j-1];
}
else if(j==0){
if(i>0) s[i][j] = s[i-1][j];
}
else s[i][j] = s[i-1][j] + s[i][j-1];
}
}
return s[m-1][n-1];
}
}
```

## Problem solution in C++.

```class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {

for(int i = 0; i < obstacleGrid.size(); i++) {
for (int j = 0; j < obstacleGrid[i].size(); j++) {
if (obstacleGrid[i][j] == 1) {
obstacleGrid[i][j] = 0;
} else if (i == 0 && j == 0) {
obstacleGrid[i][j] = 1;
} else if (i == 0) {
obstacleGrid[i][j] = obstacleGrid[i][j-1];
} else if (j == 0) {
obstacleGrid[i][j] = obstacleGrid[i-1][j];
} else {
long long int temp = (long long int)obstacleGrid[i-1][j] + (long long int)obstacleGrid[i][j-1];
if (temp > INT_MAX) {
obstacleGrid[i][j] = -1;
} else {
obstacleGrid[i][j] = temp;
}
}
}
}
return obstacleGrid[obstacleGrid.size()-1][obstacleGrid.size()-1];
}
};
```

## Problem solution in C.

```int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize) {

int i, j;
int dp;
memset(dp, 0, sizeof(dp));

for(i = 1; i <= obstacleGridRowSize; i++){
for(j = 1; j <= obstacleGridColSize; j++){
if(obstacleGrid[i - 1][j - 1] != 1 && i == 1 && j == 1){
dp[i][j] = 1;

}else if(obstacleGrid[i - 1][j - 1] != 1){
dp[i][j] = dp[i - 1][j] +  dp[i][j - 1];
}
}
}
return dp[obstacleGridRowSize][obstacleGridColSize];
}
```