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
Path With Maximum Minimum Value INPUT 3x3 Grid Matrix 5 4 5 1 2 6 7 4 6 START END Input Values: grid = [[5,4,5], [1,2,6], [7,4,6]] ALGORITHM STEPS 1 Use Max-Heap (Priority Queue) Start from (0,0) with value 5 2 Greedy Selection Always pick cell with max value 3 Track Minimum Update min value along path 4 Reach Destination Return min when at (m-1,n-1) Path Exploration Order: (0,0):5 --> (0,1):4 --> (0,2):5 (0,2):5 --> (1,2):6 --> (2,2):6 Path: 5 --> 4 --> 5 --> 6 --> 6 Min along path = 4 FINAL RESULT Optimal Path Highlighted 5 4 5 1 2 6 7 4 6 Path cells Min value OUTPUT 4 Maximum of minimum values Key Insight: Use a Max-Heap (Dijkstra-like approach) to greedily explore paths. At each step, pick the neighboring cell with the maximum value. Track the minimum value encountered. This ensures we find the path that maximizes the minimum value. Time: O(mn log(mn)), Space: O(mn) TutorialsPoint - Path With Maximum Minimum Value | Optimal Solution (Max-Heap/Dijkstra)
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