Skip to content
Decoder

Unique Paths II

Medium

Problem

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.

Example 1

grid = [[0, 0, 0],

Example 2

        [0, 1, 0],

Example 3

Input
[0, 0, 0]]
Output
2
Why
go around the middle on either side

Example 4

grid = [[0, 1],

Example 5

Input
[0, 0]]
Output
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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.