#63

Unique Paths II

specialist · 955 · lc medium +32 · verified · 44.2% accepted · 9,641 likes · top 26%

play →

Description

A robot starts at grid[0][0] and can only move right or down in an m x n grid where 1 marks an obstacle and 0 is open. Return the number of distinct paths from start to grid[m-1][n-1]. The answer fits within 2 * 109.

Example 1:

Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output: 2
Explanation: There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Example 2:

Input: obstacleGrid = [[0,1],[0,0]]
Output: 1

Code

1
2
3