/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 (empty) or 1 (obstacle), compute the number of distinct paths from the top-left cell (obstacle_matrix[0][0]) to the bottom-right cell (obstacle_matrix[m-1][n-1]). Movement is restricted to only right or down at each step. Paths cannot pass through cells containing obstacles (1). Return the total number of such unique paths.

Example 1

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

Output: 3

Explanation: There are three unique paths avoiding obstacles.

Example 2

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

Output: 2

Explanation: Two unique paths exist avoiding 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],[1,0]]

Expected: 1

Case 2

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

Expected: 0

Case 3

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

Expected: 1

Case 4

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

Expected: 5