Problem
An n x n grid gives the elevation of each square, and every value from 0 to n² - 1 appears exactly once. Rain starts, and at time t the water everywhere is at height t. You can swim from a square to a side neighbour up, down, left or right whenever both squares have elevation at most t, and swimming takes no time. Return the least time at which you can get from the top-left square to the bottom-right square.
Example 1
[[0, 2],
Example 2
- Input
- [1, 3]]
- Output
- 3
- Why
- you cannot leave the last square's height 3 out of any route
Example 3
[[0, 1, 2, 3, 4],
Example 4
[24, 23, 22, 21, 5],
Example 5
[12, 13, 14, 15, 16],
Example 6
[11, 17, 18, 19, 20],
Example 7
- Input
- [10, 9, 8, 7, 6]]
- Output
- 16
Constraints
1 <= n <= 500 <= grid[i][j] < n²Every value in the grid is unique