As reported: Adobe frontend loop (MTS-2, 2025), round 4 (manager round with DSA): a hard DP — unique paths in a grid with obstacles, where at most one obstacle may be removed. The DP was walked through verbally.
You are given an m × n grid where 0 is an empty cell and 1 is an obstacle. A robot starts at the top-left cell and must reach the bottom-right cell, moving only right or down. Normally it cannot enter obstacle cells, but you may remove at most one obstacle.
Count the distinct paths that are possible this way — that is, the right/down paths from the top-left to the bottom-right cell that pass through at most one obstacle cell. Return the count modulo 10^9 + 7.
Example 1
grid = [[0,0,0],
[0,1,0],
[0,0,0]]
Output: 6
All six right/down paths work: the two through the centre need the obstacle removed.
Example 2
grid = [[0,1],
[1,0]]
Output: 2
Example 3
grid = [[0,1,1],
[1,1,0]]
Output: 0
Every path crosses at least two obstacles.
Constraints: 1 ≤ m, n ≤ 100; grid[i][j] is 0 or 1.
Clarifications
- The start and end cells may be obstacles; such a cell counts as the one removed obstacle.
- Two paths are different if they visit a different sequence of cells; which obstacle is removed does not matter on its own.
Code runner format: one JSON value per line: grid. Implement countPaths(grid); the harness prints the count modulo 10^9 + 7.