FastPrepMaximum-Clearance Path From a Point

Maximum-Clearance Path From a Point

Google logoGoogle● HardNEW GRADPHONE SCREEN
Learn

Problem statement

You are given a rectangular binary grid, two open cells start and target, and a reference cell reference. A cell containing 1 is open and a cell containing 0 is a wall.

A path may move one cell up, down, left, or right through open cells and may not repeat a cell. Its clearance is the minimum Euclidean distance from any cell on the path to reference.

Return the maximum clearance among all paths from start to target. An answer within 10^-6 is accepted. A valid path is guaranteed to exist; reference may itself be open or blocked.

Function

maximumClearancePath(grid: int[][], start: int[], target: int[], reference: int[]) → double

Examples

Example 1

grid = [[1,1,1],[1,1,1],[1,1,1]]start = [0,0]target = [0,2]reference = [2,1]return = 2.0

The top-row path stays at least distance 2 from the reference. Any detour lower in the grid comes closer.

Example 2

grid = [[1,1,1],[1,1,1],[1,1,1]]start = [0,0]target = [2,2]reference = [1,1]return = 1.0

A boundary path avoids the reference itself but must use a cell one unit away.

Example 3

grid = [[1,0,1],[1,1,1],[0,0,1]]start = [0,0]target = [2,2]reference = [2,0]return = 1.0

The walls force the path through [1,0], whose distance from the reference is 1.

Constraints

  • 1 <= rows,cols <= 500.
  • Every grid cell is 0 or 1.
  • start, target, and reference are valid zero-based coordinates.
  • start and target are open and connected by at least one open-cell path.

More Google problems

See Google hiring insights
public double maximumClearancePath(int[][] grid, int[] start, int[] target, int[] reference) {
    // Write your code here.
}
grid[[1,1,1],[1,1,1],[1,1,1]]
start[0,0]
target[0,2]
reference[2,1]
expected2.0
Checking account…