/63. Unique Paths II

63. Unique Paths II

Medium
Arrays44.2% acceptance

Given a 2D integer matrix obstacle_matrix of size m x n, where each cell contains either 0 (free) or 1 (obstacle), compute the number of distinct paths from the top-left cell (0,0) to the bottom-right cell (m-1,n-1). Movement is allowed only to the right or down. Paths cannot pass through cells containing obstacles (1). Return the total number of such unique paths.

Example 1

Input: [[0,0],[1,0]]

Output: 1

Explanation: Only one path: down then right, avoiding the obstacle.

Example 2

Input: [[0,1,0],[0,0,0],[1,0,0]]

Output: 2

Explanation: Two paths exist avoiding obstacles.

Example 3

Input: [[0,0,1],[0,1,0],[0,0,0]]

Output: 1

Explanation: Only one path exists due to obstacles.

Constraints

  • 1 <= len(obstacle_matrix) <= 100
  • 1 <= len(obstacle_matrix[0]) <= 100
  • obstacle_matrix[i][j] in {0, 1}
  • The answer will be less than or equal to 2 * 109
Python (current runtime)

Case 1

Input: [[0,0,0],[0,0,1],[0,0,0]]

Expected: 3

Case 2

Input: [[0,1],[1,0]]

Expected: 0

Case 3

Input: [[0,0,0,0],[0,1,1,0],[0,0,0,0]]

Expected: 4

Case 4

Input: [[1,0],[0,0]]

Expected: 0

Case 5

Input: [[0]]

Expected: 1