Swim in Rising Water
Problem statement
You are given an n x n grid of integers. grid[r][c] is the height of the ground at row r and column c.
Rain starts at time 0. At time t, the water covers every cell whose height is at most t.
You start at the top-left cell (0, 0). At time t, you may swim from a cell to a cell next to it (up, down, left, or right) only if both cells have height at most t. Swimming takes no time, so you can cross any number of cells at the same moment.
Return the smallest time t at which you can reach the bottom-right cell (n - 1, n - 1).
Function
swimInWater(grid: int[][]) → intExamples
Example 1
grid = [[0,10,11,12],[1,9,14,13],[2,3,15,4],[8,7,6,5]]return = 7At time 6, the cells you can reach from the start have heights 0, 1, 2 and 3. Every cell next to them has height 7 or more, so you are stuck.
At time 7, the cell with height 7 is covered. You can swim along heights 0, 1, 2, 3, 7, 6, 5 and reach the bottom-right cell, so the answer is 7.
Example 2
grid = [[3,2],[0,1]]return = 3You cannot leave the start cell until it is covered, and its height is 3. At time 3, every cell is covered, so you can swim straight to the bottom-right cell. The answer is 3.
Example 3
grid = [[0]]return = 0The start cell is also the bottom-right cell. Its height is 0, so you are already there at time 0.
Constraints
n == grid.length == grid[i].length1 <= n <= 500 <= grid[i][j] <= n * n - 1- All values in
gridare different, so every number from0ton * n - 1appears exactly once.