Hard · Graphs

Swim in Rising Water

Given an n × n grid whose cells hold the distinct elevations 0 through n² − 1, return the least time t at which you can swim from the top-left cell to the bottom-right cell, moving only between 4-directionally adjacent cells whose elevations are at most t.

Examples

Example 1

{
  "grid": [
    [0, 2],
    [1, 3]
  ]
}

Output: answer = 3

Example 2

{
  "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]
  ]
}

Output: answer = 16

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Graphs problems