Ctrl K

Crossing at High Tide

Hard
A piece of land is split into an 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).
Example 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.

Example 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.

Example 3
heights=[[0]] → 0

The start is also the finish, and a cell of height 0 is under water from the very beginning.

Constraints
  • 1 ≤ n ≤ 50
  • heights is an n × n square matrix
  • The values of heights are exactly the numbers 0 to n² − 1, each appearing once
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRoute With the Highest Bottleneck MediumThe Gentlest Climb MediumLast Dry Crossing Day HardSteer Clear of the Sentries MediumFrame Around an Ink Blot Hard
loading Python…
Input
heights=[[0, 3], [2, 1]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 25 tests on the server, hidden ones included.