Optimize unique paths
Standard · Write code · about 12 min
A robot walks from the top-left to the bottom-right cell of grid, moving only right or down. Cells with 1 are obstacles; 0 is open.
Return the number of different paths. Use only one array as long as a row for extra memory.
Example 1
Input:
[[[0,0,0],[0,1,0],[0,0,0]]]
Output:
2
Example 2
Input:
[[[0,1],[0,0]]]
Output:
1
Only down, then right.
Constraints
- 1 ≤ rows, cols ≤ 100
- grid[r][c] is 0 or 1
- The answer fits in a 32-bit signed integer
Learn the idea first: Space optimization: rolling rows and variables, a visual lesson.
More Space optimization: rolling rows and variables problems
- Why iterate in reverse Warm-up
- Knapsack in 1-D Standard
- House robber in two variables Standard
- Trace one-row knapsack Standard
- Edit distance in O(min(m, n)) space Challenge