Dynamic programming · Medium
A grid planner counts routes through a rectangular layout with blocked cells. A route always moves one cell right or down, so it never revisits a cell. The caller needs the exact count for a layout whose total fits in a safe integer.
Return the number of clear right-and-down routes from the top-left cell to the bottom-right cell.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "grid": number[][] } containing only 0 and 1.
Fields
- grid is rectangular and contains only 0 (clear) and 1 (blocked).
Output
Return value
- Return an integer count. An empty grid or a grid with zero columns returns 0.
- A blocked start or destination returns 0. A single clear cell has one route: no moves.
- Only right and down moves are allowed.
Constraints
- 0 <= rows, columns <= 500.
- Every count reaching any cell is at most Number.MAX_SAFE_INTEGER. This explicit input promise avoids silently rounded JSON results.
Examples
Example 1
{
"grid": [
[
0,
0,
0
],
[
0,
1,
0
],
[
0,
0,
0
]
]
}Returns
2The center blocks the other routes.
Example 2
{
"grid": [
[
0
]
]
}Returns
1The empty move sequence is valid.
Required complexity
O(rows * columns) time and O(columns) extra space