Unique Paths II (Space Optimized)

Given a grid with blocked cells, return the number of valid right/down paths from the top-left to the bottom-right cell using a space-optimized DP approach. Pattern focus: Space optimization. This is the rolling-array version of obstacle-aware path counting.

Input Format

grid contains 0 for free cells and 1 for blocked cells

Output Format

number of valid paths

Constraints

  • 1 <= rows, cols <= 200

Examples

Example 1:

Input:

grid = [[0,0,0],[0,1,0],[0,0,0]]

Output:

2

Explanation:

The blocked center leaves 2 valid routes.

Example 2:

Input:

grid = [[0,0,0],[0,0,0],[0,0,0]]

Output:

6

Explanation:

An open 3x3 grid has 6 ways.

Example 3:

Input:

grid = [[0,1,0],[0,0,0],[0,0,0]]

Output:

3

Explanation:

One obstacle reduces the total to 3.

Loading...
Unique Paths II (Space Optimized) - Dp Grid