Unique Paths II
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: anm × ngrid,1for an obstacle and0for 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.
0if 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⁹
- 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
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more dynamic programming (dp) problems
- 70Climbing StairsEasysolve
- 121Best Time to Buy and Sell StockEasysolve
- 198House RobberMediumsolve
- 322Coin ChangeMediumsolve
- 300Longest Increasing SubsequenceMediumsolve
- 62Unique PathsMediumsolve
- 1143Longest Common SubsequenceMediumsolve
- 53Maximum SubarrayMediumsolve
- 918Maximum Sum Circular SubarrayMediumsolve
- 139Word BreakMediumsolve
- 120TriangleMediumsolve
- 64Minimum Path SumMediumsolve
- 97Interleaving StringMediumsolve
- 72Edit DistanceMediumsolve
- 221Maximal SquareMediumsolve
- 123Best Time to Buy and Sell Stock IIIHardsolve
- 188Best Time to Buy and Sell Stock IVHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.