Problem
Given an m x n
integer matrix grid
, return _the maximumscore of a path starting at _(0, 0)
and ending at(m - 1, n - 1)
moving in the 4 cardinal directions.
The score of a path is the minimum value in that path.
- For example, the score of the path
8 -> 4 -> 5 -> 9
is4
.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 100
0 <= grid[i][j] <= 10^9
Solution
Intuition
We want the path from (0,0) to (m-1,n-1) whose minimum value along the path is maximized. This is a classic “maximum minimum path” problem, which can be solved using a max-heap (priority queue) with BFS/DFS, or binary search + BFS/DFS/Union-Find.
Approach
Use a max-heap to always expand the path with the highest minimum value so far. For each cell, keep track of the best minimum value to reach it. Stop when we reach the bottom-right cell.
Code
C++
|
|
Go
|
|
Java
|
|
Kotlin
|
|
Python
|
|
Rust
|
|
TypeScript
|
|
Complexity
- ⏰ Time complexity:
O(mn log(mn))
- 🧺 Space complexity:
O(mn)