Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Path With Maximum Minimum Value in Python
In this problem, we need to find the maximum possible minimum value along any path from the top-left corner [0,0] to the bottom-right corner [R-1,C-1] of a matrix. A path can move in four cardinal directions (north, east, west, south), and the score is determined by the smallest value encountered along that path.
For example, if we have a path 8 ? 4 ? 5 ? 9, the path's score would be 4 (the minimum value).
Problem Example
Consider this matrix ?
| 5 | 4 | 5 |
| 1 | 2 | 6 |
| 7 | 4 | 6 |
The orange cells show the optimal path 5 ? 4 ? 5 ? 6 ? 6. The minimum value along this path is 4, which is our answer.
Algorithm Approach
We use Dijkstra's algorithm with a max-heap (implemented using negative values in Python's min-heap) to explore paths that maximize the minimum value ?
- Start from
[0,0]and use a priority queue to always explore the path with the highest minimum value first - Track visited cells to avoid cycles
- Stop when we reach the destination
[R-1,C-1] - The answer is the minimum of all values encountered in the optimal path
Implementation
import heapq
def maximumMinimumPath(matrix):
r, c = len(matrix), len(matrix[0])
# Initialize answer with minimum of start and end cells
ans = min(matrix[0][0], matrix[r-1][c-1])
# Track visited cells
visited = [[False for _ in range(c)] for _ in range(r)]
# Max-heap using negative values (priority, x, y)
heap = [(-matrix[0][0], 0, 0)]
heapq.heapify(heap)
# Four cardinal directions: up, down, right, left
directions = [(-1, 0), (1, 0), (0, 1), (0, -1)]
while heap:
neg_val, x, y = heapq.heappop(heap)
# Reached destination
if x == r-1 and y == c-1:
break
# Update minimum value in current path
ans = min(ans, matrix[x][y])
visited[x][y] = True
# Explore neighbors
for dx, dy in directions:
new_x, new_y = x + dx, y + dy
# Check boundaries and if cell is unvisited
if (0 <= new_x < r and 0 <= new_y < c and
not visited[new_x][new_y]):
heapq.heappush(heap, (-matrix[new_x][new_y], new_x, new_y))
return ans
# Test the function
matrix = [[5, 4, 5], [1, 2, 6], [7, 4, 6]]
result = maximumMinimumPath(matrix)
print(f"Maximum minimum path value: {result}")
Maximum minimum path value: 4
How It Works
The algorithm works by ?
- Priority Queue: We use a max-heap (negative values) to always explore the most promising path first
- Greedy Selection: At each step, we choose the unvisited neighbor with the highest value
- Path Tracking: We keep track of the minimum value seen so far in our current path
- Termination: We stop when we reach the destination, guaranteeing the optimal solution
Time and Space Complexity
- Time Complexity: O(R × C × log(R × C)) due to heap operations
- Space Complexity: O(R × C) for the visited matrix and heap storage
Conclusion
This problem uses Dijkstra's algorithm to find the path that maximizes the minimum value. The key insight is treating each cell's value as a priority and using a max-heap to explore the most promising paths first.
