Unique Paths II
Implement uniquePathsII
This is the same right/down path-counting question as Unique Paths, except some cells in the grid are now blocked and can't be stepped on at all. Given a grid marked with open (0) and blocked (1) cells, count how many distinct right/down paths get from the top-left corner to the bottom-right corner without ever passing through a blocked cell.
The reasoning carries over directly from the unobstructed version: the number of ways to reach an open cell is the ways that could arrive from above plus the ways that could arrive from the left. The only change is that a blocked cell contributes exactly 0 ways to reach it, no matter what arrives from either direction, since stepping onto it isn't allowed at all — and that 0 then correctly propagates forward, since no path can continue from a cell it was never able to enter. Sweeping the grid the same way, row by row, automatically accounts for every detour the obstacles force.
Example 1:
Input: grid = [[0,1,0],[0,0,0],[0,0,0]]
Output: 3
Example 2:
Input: grid = [[0,0],[0,0]]
Output: 2
Example 3:
Input: grid = [[1,0]]
Output: 0
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ grid.length, grid[0].length ≤ 50 - ●
grid[i][j] is 0 (open) or 1 (blocked)
grid =
[[0,1,0], [0,0,0], [0,0,0]]