Unique Paths II
MEDIUMDescription
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 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
Constraints:
m == obstacleGrid.lengthn == obstacleGrid[i].length1 <= m, n <= 100obstacleGrid[i][j]is0or1.
Approaches
Checkout 5 different approaches to solve Unique Paths II. Click on different approaches to view the approach and algorithm in detail.
Space-Optimized 1D Dynamic Programming
This approach optimizes the 2D DP solution's space complexity. Since calculating the number of paths for the current row only requires values from the current and previous row, we can use a single 1D array to store the DP values, reducing space from O(m*n) to O(n).
Algorithm
- Get grid dimensions
mandn. - If
obstacleGrid[0][0] == 1, return 0. - Create a 1D DP array
dpof sizen. - Initialize
dp[0] = 1. - Iterate through rows
ifrom 0 tom-1: a. Iterate through columnsjfrom 0 ton-1: i. IfobstacleGrid[i][j] == 1, setdp[j] = 0. ii. Else ifj > 0, updatedp[j] = dp[j] + dp[j-1]. - Return
dp[n-1].
We use a 1D array dp of size n. dp[j] will represent the number of paths to reach the cell in the current row at column j.
We iterate through the grid row by row. For each row i, we update the dp array. The update rule dp[j] = dp[j] + dp[j-1] works because when we are at (i, j), dp[j] still holds the value from the previous row (i-1, j), and dp[j-1] has just been updated to hold the value for (i, j-1).
- Initialization:
- Create a
dparray of sizen. - If
obstacleGrid[0][0] == 1, return 0. Otherwise, setdp[0] = 1.
- Create a
- Iteration:
- Iterate through rows
ifrom 0 tom-1. - For each row, iterate through columns
jfrom 0 ton-1. - If
obstacleGrid[i][j] == 1, there are no paths to this cell, so setdp[j] = 0. - If
j > 0, add the paths from the left cell:dp[j] += dp[j-1]. - Note: For the first cell of each row (
j=0), if it's an obstacle,dp[0]becomes 0. If not, it retains its value from the previous row, which is correct as it can only be reached from above.
- Iterate through rows
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
if (obstacleGrid[0][0] == 1) {
return 0;
}
int[] dp = new int[n];
dp[0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (obstacleGrid[i][j] == 1) {
dp[j] = 0;
} else if (j > 0) {
dp[j] = dp[j] + dp[j - 1];
}
}
}
return dp[n - 1];
}
}
Complexity Analysis
Pros and Cons
- Highly efficient in both time and space.
- Optimal space complexity among solutions that do not modify the input.
- The logic can be slightly less intuitive than the 2D DP approach.
- The 1D array is overwritten, so we lose the path counts for previous rows.
Code Solutions
Checking out 4 solutions in different languages for Unique Paths II. Click on different languages to view the code.
class Solution {
public
int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i) {
dp[i][0] = 1;
}
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j) {
dp[0][j] = 1;
}
for (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];
}
}
Video Solution
Watch the video walkthrough for Unique Paths II
Similar Questions
5 related questions you might find useful
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.