Leetcode Unique Paths II problem solution

In the Leetcode Unique Paths II problem solution You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m – 1][n – 1]). The robot can only move either down or right at any point in time.

An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.

Return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The testcases are generated so that the answer will be less than or equal to 2 * 109.

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 3×3 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

Constraints:

  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j] is 0 or 1.

Solution in C Programming

int uniquePathsWithObstacles(int** arr, int n, int* m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < *m; j++)
        {
            if (arr[i][j] == 1)
                arr[i][j] = 0;
            else if (j == 0 && i == 0)
                arr[i][j] = !arr[i][j];
            else if (i == 0)
                arr[i][j] = arr[i][j - 1];
            else if (j == 0)
                arr[i][j] = arr[i - 1][j];
            else
                arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
        }
    }
    return (arr[n - 1][*m - 1]);
}

Solution in C++ Programming

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        if(obstacleGrid[0][0] == 1)return 0;
        int n = obstacleGrid.size();
        int m = obstacleGrid[0].size();
        vector<vector<int>>dp(n,(vector<int>(m,1)));
        for(int i = 0;i < n;i++){
            if(obstacleGrid[i][0] == 1){
                while(i < n){
                    dp[i][0] = 0;
                    i++;
                }
            }
        }
        for(int j = 0;j < m;j++){
            if(obstacleGrid[0][j] == 1){
                while(j < m){
                    dp[0][j] = 0;
                    j++;
                }
            }
        }
        for(int i = 1;i < n;i++){
            for(int j = 1;j < m;j++){
                if(obstacleGrid[i][j] == 1){
                    dp[i][j] = 0;
                }
                else{
                    int up = 0,left = 0;
                    if(i > 0) up = dp[i-1][j];
                    if(j > 0) left = dp[i][j-1];
                    dp[i][j] = left + up;
                }
            }
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }
        return dp[n-1][m-1];
    }
};

Solution in Java Programming

class Solution {

    // Using tabulation
    public int uniquePathsWithObstacles(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;

        int[][] dp = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    dp[i][j] = 0;
                } else if (i == 0 && j == 0)
                    dp[i][j] = 1;
                else {
                    int up = 0, left = 0;
                    if (i > 0) up = dp[i - 1][j];
                    if (j > 0) left = dp[i][j - 1];
                    dp[i][j] = up + left;
                }
            }
        }
        return dp[n - 1][m - 1];
    }
}

Solution in Python Programming

class Solution(object):
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        dp = [[0]*n for _ in range(m)]
        
        dp[0][0] = 1 - obstacleGrid[0][0]
        for j in range(1, n):
            if obstacleGrid[0][j]!= 1:
                dp[0][j] = dp[0][j-1]
            else:
                dp[0][j] = 0
        for i in range(1, m):
            if obstacleGrid[i][0] != 1:
                dp[i][0] = dp[i-1][0]
            else:
                dp[i][0] = 0
            
        for i in range(1, m):
            for j in range(1, n):
                if obstacleGrid[i][j]!= 1:
                    dp[i][j] = dp[i-1][j] + dp[i][j-1]
                else:
                    dp[i][j] = 0
        return dp[-1][-1]

Solution in C# Programming

public class Solution {
    public int UniquePathsWithObstacles(int[][] obstacleGrid) {
        /*
        At any cell in the grid, we can only get there by going down one or to the right one.
        <=> grid[i][j] = grid[i-1][j] + grid[i][j-1]
        */
        int col = obstacleGrid.Length;
        int row = obstacleGrid[0].Length;
        
        
        // If the first or the last cell are obstacle, we don't need to run the algorithm.
        if (obstacleGrid[col-1][row-1] == 1 || obstacleGrid[0][0] == 1){
            return 0;
        }
        
        // If the grid only have 1 row or colum and there is no obstacle, return 1 else 0.
        if (col == 1 || row == 1){
            // Check the column
            if (obstacleGrid[0].Contains(1)){
                return 0;
            }
            // Check the row
            for (int i = 0; i < row; i++){
                if (obstacleGrid[0][i] == 1) {
                    return 0;
                }
            }
            return 1;
        }
       
        /*
        Fill up the first row and column with 1s unless there is an obstacle which we will
        flip the value of that to 0 as we encouter them.
        Any cell on the right of an obstacle in the first row will have a value of 0.
        Any cell below an obstacle in the first column will have a value of 0.
        */
        for (int i = 0; i < col; i++){
            // There is alredy a blocker to the left of the current cell
            if (i > 0 && obstacleGrid[i-1][0] == 0){
                obstacleGrid[i][0] = 0;
            }
            // This cell is a blocker
            else if (obstacleGrid[i][0] != 1) {
                obstacleGrid[i][0] = 1;
            }
            else {
                obstacleGrid[i][0] = 0;
            }
        }
        for (int i = 1; i < row; i++){
            // There is alredy a blocker above the current cell
            if (i > 0 && obstacleGrid[0][i-1] == 0){
                obstacleGrid[0][i] = 0;
            }
            // This cell is a blocker
            else if (obstacleGrid[0][i] != 1) {
                obstacleGrid[0][i] = 1;
            }
            else {
                obstacleGrid[0][i] = 0;
            }
        }
        
        // Looping through every cell in the grid excluding the first row and column.
        for (int i = 1; i < col; i++){
            for (int j = 1; j < row; j++){
                if (obstacleGrid[i][j] == 1){
                    obstacleGrid[i][j] = 0;
                }
                else {
                    obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
                }
            }
        }
        return obstacleGrid[col-1][row-1];
    }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *