27 · Shortest Paths

#778 Swim in Rising Water

Hardsolve on LeetCode ↗

frequently reported atMetaGoogleAmazon

the problem

In plain words

grid is n × n, grid[r][c] is the elevation, and all values are a permutation of 0..n² − 1. At time t the water depth everywhere is t, and you can swim between adjacent cells whose elevations are both ≤ t. Return the least t at which you can swim from the top-left to the bottom-right.

in grid = [[0,2],[1,3]]

out 3

You can't reach the corner until it is underwater at t = 3.

in 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]]

out 16

  • 1 ≤ n ≤ 50
  • 0 ≤ grid[i][j] < n²
  • All values are unique

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #1631
      Path With Minimum Effort
      Mediumin Shortest Paths
    2. #1011