dp[0] = 1. For each cell: obstacle → dp[c] = 0; else if c > 0, dp[c] += dp[c − 1].
Approach 1
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int n = obstacleGrid[0].length;
int[] dp = new int[n];
dp[0] = 1;
for (int[] row : obstacleGrid)
for (int c = 0; c < n; c++) {
if (row[c] == 1) dp[c] = 0;
else if (c > 0) dp[c] += dp[c - 1];
}
return dp[n - 1];
}
}
Verdict: Handles edges naturally.
Before you submit
Edge cases and common mistakes
Test these inputs
Obstacle at the start or end (0)
Obstacle in the first row blocks everything to its right
Mistakes people make
Setting the whole first row to 1 regardless of obstacles.