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 * 10^9.
There is one obstacle in the middle of a 3x3 grid as illustrated below.
1
2
3
4
5
6
Input: obstacleGrid =[[0,0,0],[0,1,0],[0,0,0]]Output: 2Explanation: 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
Recursion is straightforward. Starting from (0, 0), we attempt to move right or down unless an obstacle or boundary is encountered. If the destination is reached, count it as one unique path.
publicclassSolution {
publicintuniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
return dfs(obstacleGrid, 0, 0, m, n);
}
privateintdfs(int[][] grid, int i, int j, int m, int n) {
if (i >= m || j >= n || grid[i][j]== 1) { // Out of bounds or obstaclereturn 0;
}
if (i == m - 1 && j == n - 1) { // Reached destinationreturn 1;
}
return dfs(grid, i + 1, j, m, n) + dfs(grid, i, j + 1, m, n); // Move down or right }
}
For top-down DP, we introduce a memo array to remember results of subproblems to avoid recalculating them. At each cell (i, j), we calculate the number of paths recursively and store the result in memo[i][j]. If we’ve already computed the number of paths from that cell, we just return the stored value.
publicclassSolution {
publicintuniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
return dfs(obstacleGrid, 0, 0, m, n, new Integer[m][n]);
}
privateintdfs(int[][] grid, int i, int j, int m, int n, Integer[][] memo) {
if (i >= m || j >= n || grid[i][j]== 1) { // Out of bounds or obstaclereturn 0;
}
if (i == m - 1 && j == n - 1) { // Reached destinationreturn 1;
}
if (memo[i][j]!=null) {
return memo[i][j];
}
return memo[i][j]= dfs(grid, i + 1, j, m, n, memo) + dfs(grid, i, j + 1, m, n, memo); // Move down or right }
}
publicclassSolution {
publicintuniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp =newint[m][n];
// If starting cell has an obstacle, there is no path.if (obstacleGrid[0][0]== 1) {
return 0;
}
// Initialize starting cell dp[0][0]= 1;
// Fill out the first columnfor (int i = 1; i < m; i++) {
if (obstacleGrid[i][0]== 0 && dp[i - 1][0]== 1) {
dp[i][0]= 1;
}
}
// Fill out the first rowfor (int j = 1; j < n; j++) {
if (obstacleGrid[0][j]== 0 && dp[0][j - 1]== 1) {
dp[0][j]= 1;
}
}
// Iterate through grid and populate number of paths for each cellfor (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (obstacleGrid[i][j]== 0) {
dp[i][j]= dp[i - 1][j]+ dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1]; // Bottom-right corner has the final count }
}
This is a typical 2D DP problem, we can store value in 2D DP array, but since we only need to use value at dp[i - 1][j] and dp[i][j - 1] to update dp[i][j], we don’t need to store the whole 2D table, but instead store value in an 1D array, and update data by using dp[j] = dp[j] + dp[j - 1], (where here dp[j] corresponding to the dp[i - 1][j]) and dp[j - 1] corresponding to the dp[i][j - 1] in the 2D array)
classSolution {
publicintuniquePathsWithObstacles(int[][] obstacleGrid) {
//Empty caseif (obstacleGrid.length== 0) return 0;
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (obstacleGrid[i][j]== 1)
obstacleGrid[i][j]= 0;
elseif (i == 0 && j == 0)
obstacleGrid[i][j]= 1;
elseif (i == 0)
obstacleGrid[i][j]= obstacleGrid[i][j - 1]; // For row 0, if there are no paths to left cell, then its 0,else 1elseif (j == 0)
obstacleGrid[i][j]= obstacleGrid[i - 1][j]; // For col 0, if there are no paths to upper cell, then its 0,else 1else obstacleGrid[i][j]= obstacleGrid[i - 1][j]+ obstacleGrid[i][j - 1];
}
}
return obstacleGrid[rows - 1][cols - 1];
}
}