Problem
You are given an m x n integer grid grid.
You start at the top-left cell (0, 0) and want to reach the bottom-right cell (m-1, n-1).
You may move one step at a time in 4 directions: up, down, left, right (no diagonals). You may not go outside the grid.
Define a threshold value T. A path is valid under threshold T if every cell value on the path is <= T.
Task
Return the minimum integer T such that there exists at least one valid path from (0,0) to (m-1,n-1) under threshold T.
Input
-
grid
: a 2D array of integers with dimensions
m x n
.
Output
-
An integer: the minimum
T
that makes the destination reachable.
Notes / Constraints (assume)
-
1 <= m, n <= 200
-
Cell values are integers (may be large; e.g., up to
10^9
in absolute value).
-
Movement is 4-directional.
Example
If grid = [[0,2],[1,3]], the answer is 3 because you cannot reach the bottom-right without allowing the cell value 3.