Skip to content

Practice Unique Paths III

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#980
LeetCode Problem

Unique Paths III

Problem Statement

The grid uses 1 for its single starting cell, 2 for its single ending cell, 0 for other walkable cells and -1 for obstacles. Count the paths from start to end that move horizontally or vertically and visit every non-obstacle cell exactly once.

Source: Unique Paths III on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output
2

# Example 2

Input
grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output
4

# Example 3

Input
grid = [[0,1],[2,0]]
Output
0

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 20
1 <= m * n <= 20
-1 <= grid[i][j] <= 2
There is exactly one starting cell and one ending cell.

Related LeetCode Problems

Practice more problems similar to Unique Paths III.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.