# Unique Paths II

Difficulty Level Medium
Dynamic Programming MatrixViews 726

Suppose a man standing in the first cell or the top left corner of “a × b” matrix. A man can move only either up or down. That person wants to reach his destination and that destination for him is the last cell of the matrix or bottom right corner.

And if there are some hurdles in it, how many unique paths can be identified by the person. Help him to reach the destination by knowing No of Unique Paths.

A hurdle is considered as 1 and space is marked as 0 in the matrix.

## Example

Input:

[
[0,0,0],
[0,1,0],
[0,0,0]
]

Output:

2

Explanation:

Because only one hurdle is present in the middle of the whole matrix which allows only two unique paths:-

1. Down →Down →Right →Right
2. Right →Right →Down →Down In the above image, the blue arrow shows path 1 and the red arrow shows path 2.

## Algorithm

1. Check if the first cell that is array contains 1 then return the unique paths as 0 as it can’t move forward than this.
2. If array doesn’t contain the value 1 then initialize the value of array=1.
3. Now, iterate over the first row of the array and if this cell contains 1, this means the current cell has a hurdle in it and sets the value of a cell to 0. Else set the value of this cell as of the previous cell that is array[i][j]= array[i][j-1].
4. Now, iterate over the first column of the array and if this cell contains 1, this means the current cell has a hurdle in it and sets the value of the cell to 0. Else set the value of this cell as of the previous cell that is array[i][j]= array[i][j-1].
5. Now, iterate over the whole matrix starting from the cell array.
6. Check if cell doesn’t contain any hurdle then do arrayi,j] = array [i-1,j] + array [i,j-1].
7. If a cell contains a hurdle, then set the value of the cell to 0 to make sure it doesn’t repeat in any other path.

## Explanation

So our main idea for solving this question is if a cell doesn’t have the values 0 in it then it means that we have the number of ways of reaching our destination cell is the sum of a number of ways of reaching the cell above that cell and sum of the number of ways of reaching the cell left of that cell.

So we implemented some of the functions which are going to help us in solving our problem. We declared a function getVal, getVal gets some arguments perform some operations and return some value, that is going to solve the following problem:

1. If in a first row, a cell contains a hurdle it sets the value to 0 else it will set the value of that cell as that of the previous cell that is array[i][j]=array[i][j-1].
2. If in a first row, a cell contains a hurdle it sets the value to 0 else it will set the value of that cell as that of the previous cell that is array[i][j]=array[i-1][j].

### Example

So let us take an example as Array={{0,0,0},{0,1,0},{0,0,0}};

An array is passed into findUniquePath

Array={{0,0,0},{0,1,0},{0,0,0}};

So its first cell that is array is not equal to 1.

So set, array=1;

Iterating over column,

i=1

if array==0 and array=1 is true

so array=1;

i=2;

if array==0 and array=1 is true

so array=1;

Now iterating over row,

i=1

if array==0 and array=1 is true

so array=1;

i=2;

if array==0 and array=1 is true

so array=1;

Now starting from array

i=1,j=1

if array==0 is false

so array=0

i=1,j=2

if array==0 is true

array[i][j] = array[i – 1][j] + array[i][j – 1];

so array=array+array;

array=1+1=2;

i=2,j=1

if array==0 is true

so array=0

i=2,j=2

if array==0 is true

array[i][j] = array[i – 1][j] + array[i][j – 1];

so array=array+array;

array=2+0;

array=2

That is we are going to return the value array which means 2 is our required output.

## Implementation

### C++ program for Unique Paths II

```#include<iostream>
using namespace std;
int getVal(int x, int y)
{
if(x==0 && y==1)
{
return 1;
}
else
{
return 0;
}
}
int findUniquePath(int arr)
{
if (arr == 1)
{
return 0;
}
arr = 1;
for (int i = 1; i < 3; i++)
{
arr[i] = getVal(arr[i],arr[i-1]);
}
for (int i = 1; i < 3; i++)
{
arr[i] = getVal(arr[i],arr[i-1]);
}
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 3; j++)
{
if (arr[i][j] == 0)
{
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
else
{
arr[i][j] = 0;
}
}
}
return arr;
}
int main()
{
int inputValue= {{0,0,0},
{0,1,0},
{0,0,0}};

findUniquePath(inputValue);
cout<<findUniquePath(inputValue);;
return 0;
}
```
`2`

### Java program for Unique Paths II

```class uniquePath2 {
public static int getVal(int x, int y) {
if (x == 0 && y == 1) {
return 1;
} else {
return 0;
}
}
public static int findUniquePath(int array[][]) {
if (array == 1) {
return 0;
}
array = 1;
for (int i = 1; i<array.length; i++) {
array[i] = getVal(array[i], array[i - 1]);
}
for (int i = 1; i<array.length; i++) {
array[i] = getVal(array[i], array[i - 1]);
}
for (int i = 1; i<array.length; i++) {
for (int j = 1; j<array[i].length; j++) {
if (array[i][j] == 0) {
array[i][j] = array[i - 1][j] + array[i][j - 1];
} else {
array[i][j] = 0;
}
}
}

int row = array.length;
int col = array.length;
return array[row - 1][col - 1];
}
public static void main(String[] args) {
int inputValue[][] = {
{
0, 0, 0
}, {
0, 1, 0
}, {
0, 0, 0
}
};
System.out.println(findUniquePath(inputValue));
}
}```
`2`

## Complexity Analysis for Unique Paths II

### Time Complexity

O(a × b) where a and b is the number of rows and columns in the matrix given to us as each cell is processed only once.

### Space Complexity

O(1) as no extra space is being utilized since we are using dynamic programming.

Reference

Translate »