FastPrepMinimum Flood Level to Disconnect a Grid

Minimum Flood Level to Disconnect a Grid

Meta logoMeta● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

An elevation grid has a start at its top-left cell and a destination at its bottom-right cell. At integer water level L, every cell with elevation at most L is flooded and unavailable. A route can move one cell up, down, left, or right, and may use only unflooded cells.

Find the smallest nonnegative water level for which no route remains from start to destination. Flooding either endpoint also removes every route. Water rises to the stated level inclusively: a cell of elevation L is already flooded at level L.

Function

minimumFloodLevel(elevation: int[][]) → int

Examples

Example 1

elevation = [[5,4],[3,6]]return = 4

At level 3, the route through elevation 4 remains. At level 4, both possible routes are cut.

Example 2

elevation = [[8,2,7],[6,5,4],[3,9,10]]return = 5

The strongest remaining route runs through elevations 8, 6, 5, 9, and 10. Flooding level 5 cuts it.

Example 3

elevation = [[0,9],[8,10]]return = 0

The start is flooded at the first allowed water level, 0.

Constraints

  • 1 <= elevation.length, elevation[r].length <= 40; all rows have equal length.
  • 0 <= elevation[r][c] <= 1000000. Equal elevations are allowed.
  • Return an integer from 0 through the maximum cell elevation.

More Meta problems

See Meta hiring insights
public int minimumFloodLevel(int[][] elevation) {
    // Write your code here
}
elevation[[5,4],[3,6]]
expected4
Checking account…