Description

Given an n x n grid where grid[i][j] represents elevation at that point, return the minimum time when you can swim from (0,0) to (n-1,n-1). At time t, you can swim where elevation ≤ t.

Examples

Input:grid = [[0,2],[1,3]]
Output:3
Explanation:

At time 3, path (0,0) → (1,0) → (1,1) is passable.

Input:grid = [[1]]
Output:1
Explanation:

Minimal case with a single-element matrix.

Input:grid = [[0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8]]
Output:8
Explanation:

In this 5x5 grid with increasing elevations, the goal is to reach corner (4,4) which has elevation 8. The optimal path follows a route where the maximum elevation encountered is minimized, requiring waiting until time 11.

Constraints

  • n == grid.length == grid[i].length
  • 1 ≤ n ≤ 50

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!