Skip to main content

Path With Maximum Minimum Value - Solution & Explanation

MediumPremiumFree on FleetCodeArrayBinary SearchDepth-First SearchBreadth-First Search18 min readAsked at: Amazon, Google, Geico
Practice this problem

Problem Statement

Given an m x n integer matrix grid, return the maximum score 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 is 4.

 

Example 1:

Input: grid = [[5,4,5],[1,2,6],[7,4,6]]
Output: 4
Explanation: The path with the maximum score is highlighted in yellow. 

Example 2:

Input: grid = [[2,2,1,2,2,2],[1,2,2,2,1,2]]
Output: 2

Example 3:

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],[4,6,5,4,3]]
Output: 3

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 100
  • 0 <= grid[i][j] <= 109

Approach Overview

Problem Overview: Given an m × n grid, you start at (0,0) and must reach (m-1,n-1). Every path has a score defined by the minimum value encountered along that path. The goal is to choose the path whose minimum value is as large as possible.

Approach 1: Max Heap (Priority Queue) Search (O(mn log(mn)) time, O(mn) space)

This approach is a variation of Dijkstra’s algorithm on a grid. Instead of minimizing distance, you maximize the minimum value seen so far. Use a max heap (priority queue) that always expands the cell with the highest value first. Track the path score as the minimum value along the current path. Each time you pop a cell, update the score and push its four neighbors if they are unvisited. The first time you reach the bottom-right cell, the recorded score is the best possible. The greedy ordering works because exploring higher values first guarantees no later path can produce a better minimum.

This method resembles graph traversal using BFS with a priority queue. It is easy to implement and performs well for typical grid sizes.

Approach 2: Sorting + Union-Find (O(mn log(mn)) time, O(mn) space)

Think about the problem in reverse: instead of building a path, gradually allow cells from highest value to lowest. Sort all grid cells in descending order by value. As you activate each cell, connect it with its already-active neighbors using a Union-Find structure. The moment the start cell and end cell become connected, the current value is the maximum possible minimum value of a valid path.

The key insight: if you only allow cells with value ≥ x, you are effectively checking whether a path exists where every cell meets that threshold. Activating cells from largest to smallest guarantees that the first successful connection yields the optimal answer.

This approach turns the grid into a connectivity problem. Union-Find keeps the operations near constant time using path compression and union by rank.

Recommended for interviews: The max-heap search is the most intuitive and closest to classic graph algorithms, so many candidates reach it first. The sorting + Union-Find method demonstrates deeper insight into connectivity and threshold problems. Showing the heap approach first and then discussing the Union-Find optimization signals strong problem-solving range.

Approach 1: Sorting + Union-Find

First, we construct a triplet (v, i, j) for each element in the matrix, where v represents the element value, and i and j represent the row and column of the element in the matrix, respectively. Then we sort these triplets in descending order by element value and store them in a list q.

Next, we take out the triplets from q in order, use the corresponding element value as the score of the path, and mark the position as visited. Then we check the four adjacent positions (up, down, left, and right) of this position. If an adjacent position has been visited, we merge this position with the current position. If we find that the position (0, 0) and the position (m - 1, n - 1) have been merged, we can directly return the score of the current path as the answer.

The time complexity is O(m times n times (log (m times n) + \alpha(m times n))), where m and n are the number of rows and columns of the matrix, respectively.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Approach 2: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sorting + Union-Find
Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Max Heap (Priority Queue) Grid SearchO(mn log(mn))O(mn)General solution. Intuitive if you think of the grid as a graph and apply Dijkstra-style traversal.
Sorting + Union-FindO(mn log(mn))O(mn)Best when reasoning about connectivity thresholds. Efficient and elegant for grid activation problems.

Video Solution

1102. Path With Maximum Minimum Value | Medium | EnglishSimple Code - 단순코드4,484 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Path With Maximum Minimum Value easy or hard?
The problem is rated Medium because the grid traversal itself is straightforward, but recognizing the max-min optimization requires a greedy insight. Understanding Dijkstra-style traversal or Union-Find threshold activation is the key challenge.
How to solve Path With Maximum Minimum Value in O(mn log(mn))?
Use a priority queue to always process the highest-value cell first. Maintain the minimum value seen along the current path and mark visited cells to avoid revisiting them. When the bottom-right cell is reached, the recorded minimum value is the maximum possible path score.
What is the best approach for Path With Maximum Minimum Value?
The most common approach uses a max heap (priority queue) to perform a Dijkstra-style traversal on the grid. Always expand the cell with the highest value and track the minimum value along the path. This guarantees the optimal answer when the destination is reached, with time complexity O(mn log(mn)) and space complexity O(mn).
Is Path With Maximum Minimum Value asked at Google/Amazon/Meta?
Path With Maximum Minimum Value appears in interview prep lists for companies like Google, Amazon, and Meta because it combines graph traversal with greedy reasoning. It tests understanding of priority queues, grid graphs, and connectivity strategies such as Union-Find.
What data structure is used in Path With Maximum Minimum Value?
The main data structures are a max heap (priority queue) for greedy traversal and Union-Find for connectivity tracking. The grid itself is treated as a graph where each cell connects to its four neighbors.
What is the time complexity of Path With Maximum Minimum Value?
Both common optimal solutions run in O(mn log(mn)) time for an m × n grid. The heap-based search performs priority queue operations for each cell, while the Union-Find approach sorts all cells and performs near-constant union operations.
Path With Maximum Minimum Value Python or Java solution approach?
In Python or Java, the typical implementation uses a priority queue. Python uses heapq with negated values to simulate a max heap, while Java uses PriorityQueue with a custom comparator. Both implementations run in O(mn log(mn)) time and require O(mn) space.

Ready to solve this problem?

Practice Path With Maximum Minimum Value with our built-in code editor and test cases.

Practice on FleetCode