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

Leetcode Unique Paths II problem solution

YASH PAL, 31 July 202418 January 2026

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

Leetcode Unique Paths II 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]

Unique Paths II 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 solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes