Imagine you're a treasure hunter navigating through a dangerous dungeon represented by an m × n grid. Each cell contains a number representing the safety level of that location. You must find a path from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1), moving only up, down, left, or right.

Here's the catch: your journey is only as safe as its weakest point. The score of any path is the minimum value encountered along that route. Your goal is to find the path that maximizes this minimum value.

Example: If you traverse cells with values 8 → 4 → 5 → 9, your path score is 4 (the minimum). You want to find the path where this minimum is as large as possible!

Input & Output

example_1.py — Basic 3x3 Grid
$ Input: grid = [[5,4,5],[1,2,6],[7,4,6]]
Output: 4
💡 Note: The optimal path is 5 → 4 → 6 → 6 with minimum value 4. Alternative paths like 5 → 1 → 2 → 6 → 6 have minimum value 1, which is worse.
example_2.py — Simple 2x3 Grid
$ Input: grid = [[2,2,1],[2,2,2]]
Output: 2
💡 Note: The path 2 → 2 → 2 → 2 gives minimum value 2. We cannot do better since we must end at the bottom-right corner.
example_3.py — Single Path Grid
$ Input: grid = [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0]]
Output: 3
💡 Note: The optimal path maintains a minimum value of 3 by carefully navigating through higher-value cells and avoiding the zeros at the bottom-right area.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 100
  • 0 ≤ grid[i][j] ≤ 109
  • You can move in 4 directions: up, down, left, right

Visualization

Tap to expand
START46END121Optimal PathMin value: 4Alternative PathMin value: 1 (worse)Key Insight: Bottleneck OptimizationYour journey is only as safe as its most dangerous pointDijkstra's algorithm finds the path where this danger is minimizedAlways explore the safest-looking option first!
Understanding the Visualization
1
Understanding the Problem
Each path's score is its weakest link - we want to maximize this minimum value
2
Why Dijkstra Works
Instead of finding shortest path, we find the path that maximizes the bottleneck value
3
Priority Queue Strategy
Always explore the most promising path first - the one with highest minimum so far
4
Optimal Solution Found
First time we reach destination gives us the optimal maximum minimum value
Key Takeaway
🎯 Key Insight: This is a bottleneck shortest path problem - use Dijkstra's algorithm with max-heap to find the path that maximizes the minimum value encountered!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen