Unique Paths II
You are given an m x n grid. A robot starts at the top-left corner of the grid and wants to reach the bottom-right corner.
The robot can only move:
- Right
- Down
Some cells contain obstacles:
- 0 → empty cell
- 1 → obstacle
The robot cannot move onto an obstacle cell.
Return the number of possible unique paths from the top-left corner to the bottom-right corner.
If the starting cell or ending cell is an obstacle, return 0.
Example 1
Input
m = 3
n = 3
obstacleGrid =
[
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
]Output
2
Explanation
There are two valid paths from the top-left to the bottom-right.
The center cell is an obstacle, so paths cannot pass through it.
Example 2
Input
m = 2
n = 2
obstacleGrid =
[
[0, 1],
[0, 0]
]Output
1
Explanation
The right cell in the first row is an obstacle, so the robot cannot move right from the starting cell.
The only valid path is:
Constraints
Hints:
Hint 1
For an empty cell, the number of ways to reach it comes from:
- The cell above
- The cell to the left
Hint 2
If the current cell is an obstacle, the number of ways to reach it is:
0
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 23, 2026
Last updated
Aug 24, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output