You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j).
The rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares is at most t.
You start at the top-left square (0, 0). Return the least time until you can reach the bottom-right square (n - 1, n - 1).
You can swim infinitely fast (moving between squares takes no time). You are simply waiting for the water to rise high enough.
grid = [[0,2],[1,3]]3grid = [[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]]16n == grid.lengthn == grid[i].length1 <= n <= 500 <= grid[i][j] < n^2Each value in grid is uniquegrid[0][0] and grid[n-1][n-1] are not necessarily 0 or n*n - 1Expected time complexity: O(n² log n)Run your code to see results
Use Cmd+Enter to run