Skip to content
Decoder

Swim in Rising Water

Hard

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 <= 50
  • 0 <= grid[i][j] < n²
  • Every value in the grid is unique

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.