Unique Paths II

Medium#632d table

A grid has free cells (0) and rocks (1). A robot starts in the top-left corner and steps only right or down, never onto a rock. Count the routes to the bottom-right corner. Our example: [0, 0, 1, 0], [0, 0, 0, 0], [0, 1, 0, 0], 4 paths.

input
obstacleGrid: an m × n grid, 1 for an obstacle and 0 for a free cell.
returns
How many paths lead from the top-left cell to the bottom-right one, moving only right or down and never onto an obstacle. 0 if an obstacle stands on the start or the finish.
constraints
  • 1 ≤ m = obstacleGrid.length ≤ 100
  • 1 ≤ n = obstacleGrid[i].length ≤ 100
  • obstacleGrid[i][j] is 0 or 1
  • the answer is at most 2·10⁹
our 14 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
function uniquePathsWithObstacles(obstacleGrid) {

}
Ctrl ↵
format
reset

saved in this browser

14 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more dynamic programming (dp) problems

Pattern “Dynamic programming (DP)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.