Find a path from the top-left to the bottom-right of a grid that maximizes the minimum distance to any dangerous cell along the route.
You are given an grid where some cells are dangerous and the rest are safe. A path starts at the top-left cell and ends at the bottom-right cell, moving only in the four cardinal directions and never leaving the grid.
For any cell on a path, define its safeness as the Manhattan distance to the nearest dangerous cell. The safeness factor of a path is the minimum safeness among all cells on that path.
Return the largest possible safeness factor among all valid paths from to .
1, and safe cells with 0.n, the size of the square grid.n x n binary grid, where 1 denotes a dangerous cell and 0 denotes a safe cell.0 or 1.Example 1
Input
grid = [[1,0,0],[0,0,0],[0,0,1]]
Output
0
Explanation
The start cell and the end cell are dangerous, so any valid path must include a cell with safeness 0. Therefore the best achievable safeness factor is 0.
Example 2
Input
grid = [[0,0,1],[0,0,0],[1,0,0]]
Output
1
Explanation
Every path from the top-left to the bottom-right can stay at least distance 1 away from the nearest dangerous cell, and no path can guarantee distance 2 throughout.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.