FastPrepSwim in Rising Water

Swim in Rising Water

Google logoGoogle● HardNEW GRADPHONE SCREEN
Learn

Problem statement

A rectangular grid contains distinct integer heights. Start at the top-left cell and move up, down, left, or right until reaching the bottom-right cell.

The cost of a path is the maximum height on that path. Return the minimum possible path cost.

Function

swimInWater(grid: int[][]) → int

Examples

Example 1

grid = [[0,2],[1,3]]return = 3

Every route ends at height 3, so the minimum possible maximum is 3.

Example 2

grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]return = 16

The winding low-height route reaches the target without using a cell above 16.

Example 3

grid = [[7]]return = 7

The only path contains the start cell itself.

Constraints

  • 1 <= grid.length, grid[i].length <= 500.
  • Every row has the same length.
  • The grid has at most 200000 cells.
  • All heights are distinct integers in [0, 1000000000].

More Google problems

See Google hiring insights
public int swimInWater(int[][] grid) {
    // Write your code here.
}
grid[[0,2],[1,3]]
expected3
Checking account…