Swim in Rising Water
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[][]) → intExamples
Example 1
grid = [[0,2],[1,3]]return = 3Every 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 = 16The winding low-height route reaches the target without using a cell above 16.
Example 3
grid = [[7]]return = 7The 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
200000cells. - All heights are distinct integers in
[0, 1000000000].