Math Fundamentals18 sections · 814 units
Open in Course

Unique Paths II - Handling Obstacles

(Setting blocked cells to 0)

If a cell has an obstacle, the number of paths through that cell is 0. You cannot reach it or pass through it.

Critical: If the starting cell or ending cell has an obstacle, return 0 immediately. No paths exist.

For other cells: if obstacle, set dp[i][j]=0dp[i][j] = 0. Otherwise, use the same formula: dp[i][j]=dp[i1][j]+dp[i][j1]dp[i][j] = dp[i-1][j] + dp[i][j-1].