n × n grid, and heights[i][j] is the height of each cell. All heights are different.
The tide is coming in: at time t the water stands at level t across the whole grid. You may only be in a cell whose height is at most t. You can move to a cell sharing a side with yours when both cells are under water; swimming any distance takes no time, but you cannot leave the grid.
You start in the top-left corner (0, 0). Return the smallest time t at which you can reach the bottom-right corner (n - 1, n - 1).heights=[[0, 3], [2, 1]] → 2
The bottom-right cell has height 1, but both of its neighbours are higher: the way through (1, 0) opens at level 2 and the way through (0, 1) at level 3. So the answer is 2.
heights=[[0, 1, 2], [7, 8, 3], [6, 5, 4]] → 4
The route follows the edge: 0 → 1 → 2 → 3 → 4. Its highest cell is 4, and the finish cannot be reached any earlier.
heights=[[0]] → 0
The start is also the finish, and a cell of height 0 is under water from the very beginning.
heights is an n × n square matrixheights are exactly the numbers 0 to n² − 1, each appearing onceheights=[[0, 3], [2, 1]]
2
Run checks the first 3 tests in your browser. Submit checks all 25 tests on the server, hidden ones included.