Unique Paths II - Complete Solution Guide
Unique Paths II is LeetCode problem 63, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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-ri
Detailed Explanation
The problem asks us to find the number of unique paths a robot can take from the top-left corner to the bottom-right corner of an m x n grid. The robot can only move down or right. The grid contains obstacles marked as '1', which the robot cannot step on. The goal is to return the count of paths avoiding the obstacles.
Solution Approach
The provided code utilizes dynamic programming with space optimization. It creates a 1D array `dp` of size `n` to store the number of paths to reach each cell in the current row. The algorithm iterates through the grid row by row and column by column. If a cell contains an obstacle, the corresponding `dp` value is set to 0. Otherwise, the `dp` value is updated by adding the `dp` value of the cell to its left (if it exists). The initial `dp[0]` is set to 1, representing the starting point.
Step-by-Step Algorithm
- Step 1: Initialize a 1D array `dp` of size `n` with all elements set to 0. Set `dp[0]` to 1, indicating one way to reach the starting cell (top-left). If the starting cell has an obstacle, return 0 immediately.
- Step 2: Iterate through each row `i` from 0 to `m-1` of the grid.
- Step 3: Inside each row, iterate through each column `j` from 0 to `n-1`.
- Step 4: If the current cell `obstacleGrid[i][j]` is an obstacle (value is 1), set `dp[j]` to 0, indicating no path can reach this cell.
- Step 5: If the current cell is not an obstacle and `j > 0`, update `dp[j]` by adding `dp[j-1]` to it. This represents adding the number of paths from the left cell to the current cell's path count.
- Step 6: After iterating through all rows and columns, `dp[n-1]` will contain the number of unique paths to reach the bottom-right corner. Return this value.
Key Insights
- Insight 1: Dynamic Programming: The number of ways to reach a cell is the sum of the number of ways to reach the cell above it and the number of ways to reach the cell to its left.
- Insight 2: Obstacle Handling: If a cell contains an obstacle, the number of paths to reach that cell is 0.
- Insight 3: Space Optimization: We can solve this problem using O(n) space instead of O(m*n) by only storing the current row's path counts.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(n)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: Adobe, Agoda, Amazon, Apple, Atlassian, Bloomberg, Cisco, Coupang, Cruise, Flipkart, Meta, Microsoft, Pinterest, Uber, Yahoo, Zepto, Zomato, athenahealth.