Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

Leetcode Unique Paths II problem solution

YASH PAL, 31 July 2024

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.

leetcode unique paths ii problem solution

Problem solution in Python.

class Solution:
    def uniquePathsWithObstacles(self, og: List[List[int]]) -> int:
        if og[0][0]==1:
            return 0
        else:
            og[0][0]=1
        for i in range(1,len(og)):
            if og[i][0]!=1:
                og[i][0]=og[i-1][0]
            else:
                og[i][0]=0
        for i in range(1,len(og[0])):
            if og[0][i]!=1:
                og[0][i]=og[0][i-1]
            else:
                og[0][i]=0
        for i in range(1,len(og)):
            for j in range(1,len(og[0])):
                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[0].length;
    int[][] s = new int[m][n];
    s[0][0] = obstacleGrid[0][0]==0 ? 1:0;
    if(s[0][0] == 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[0].size()-1];
    }
};

Problem solution in C.

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

int i, j;
int dp[101][101];
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];
}

coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programmingoneonone | WordPress Theme by SuperbThemes