You are given an m x n grid heights of distinct integers representing terrain heights. You start at the top-left cell (0,0) and want to reach the bottom-right cell (m-1,n-1) by moving 4-directionally (up, down, left, right) within the grid.
Define the cost of a path as the maximum height value among all cells on that path.
Task
Return the minimum possible path cost, i.e., among all valid paths from (0,0) to (m-1,n-1), minimize the maximum height encountered.
Input
-
heights
:
m x n
integer grid
Output
-
An integer: the minimum achievable value of
max(height on path)
Notes / Follow-ups discussed
-
A typical approach is to model this as a shortest-path variant (e.g., Dijkstra where path “distance” is
minimize max-so-far
).
-
Follow-up: Can this be implemented with DFS? If yes, what approach would you use (plain DFS vs DFS + binary search vs memoization), and what is the time complexity?