Skip to content

Unique Paths II

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

A robot starts at the top-left of a grid and wants to reach the bottom-right, moving only right or down. Some cells are blocked: grid[r][c] == 1 is an obstacle the robot cannot enter, and 0 is open. Return how many different paths reach the bottom-right cell.

grid = [[0, 0, 0],
        [0, 1, 0],
        [0, 0, 0]]   →  2   (go around the middle on either side)
 
grid = [[0, 1],
        [0, 0]]      →  1

If the start or the finish is an obstacle, the answer is 0.

Constraints

  • 1 <= m, n <= 100 where the grid is m × n
  • grid[r][c] is 0 or 1
  • The answer is at most 2 * 10^9.