7.Unique Paths

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).

Input: m = 3, n = 7
Output: 28
Example 2:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down

Example 3:
Input: m = 7, n = 3
Output: 28

Example 4:
Input: m = 3, n = 3
Output: 6

Solution : (Using DP)

Approach: For 1st row and column there is only 1 possible path For Other, it is sum of left and top

class Solution
{
public:
    int uniquePaths(int m, int n)
    {

        int a[m][n];

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (i == 0 || j == 0)
                {
                    a[i][j] = 1;
                }
                else
                {
                    a[i][j] = a[i][j - 1] + a[i - 1][j];
                }
            }
        }
        return a[m - 1][n - 1];
    }
};

Time Complexity: O(MN) , Space Complexity: O(MN)

Unique Paths II

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).

Example 1:

Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output: 2
Explanation: There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Example 2:

Input: obstacleGrid = [[0,1],[0,0]]
Output: 1

Now consider if some obstacles are added to the grids. How many unique paths would there be?

Example 1:

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2

Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Solution:

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

        int n = obstacleGrid.size();
        int m = obstacleGrid[0].size();
        int arr[n][m];
        
        if (obstacleGrid[0][0] == 1)
        {
            arr[0][0] = 0;
        }
        else
        {
            arr[0][0] = 1;
        }
        
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (i == 0 && j == 0)
                {
                    continue;
                }
                
                if ((i == 0 || j == 0) && obstacleGrid[i][j] != 1)
                {
                    if (i == 0)
                    {
                        if (arr[i][j - 1] == 0)
                        {
                            arr[i][j] = 0;
                        }
                        else
                        {
                            arr[i][j] = 1;
                        }
                    }
                    else if (j == 0)
                    {
                        if (arr[i - 1][j] == 0)
                        {
                            arr[i][j] = 0;
                        }
                        else
                        {
                            arr[i][j] = 1;
                        }
                    }
                }
                else if (obstacleGrid[i][j] == 1)
                {
                    arr[i][j] = 0;
                }
                else
                {
                    arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
                }
            }
        }

        return arr[n - 1][m - 1];
    }
};

Time Complexity: O(MN) , Space Complexity: O(MN)

Last updated

Was this helpful?