import heapq
class Solution:
def maximumMinimumPath(self, grid):
m, n = len(grid), len(grid[0])
visited = [[False]*n for _ in range(m)]
# Max heap: use negative values because heapq is min-heap by default
heap = [(-grid[0][0], 0, 0)]
directions = [(0,1), (1,0), (0,-1), (-1,0)]
visited[0][0] = True
while heap:
val, x, y = heapq.heappop(heap)
val = -val
if x == m-1 and y == n-1:
return val
for dx, dy in directions:
nx, ny = x+dx, y+dy
if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny]:
visited[nx][ny] = True
heapq.heappush(heap, (-min(val, grid[nx][ny]), nx, ny))
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
class Solution {
public:
int maximumMinimumPath(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<bool>> visited(m, vector<bool>(n, false));
priority_queue<tuple<int,int,int>> pq;
pq.emplace(grid[0][0], 0, 0);
visited[0][0] = true;
vector<pair<int,int>> dirs = {{0,1},{1,0},{0,-1},{-1,0}};
while (!pq.empty()) {
auto [val, x, y] = pq.top(); pq.pop();
if (x == m-1 && y == n-1) return val;
for (auto& d : dirs) {
int nx = x + d.first, ny = y + d.second;
if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny]) {
visited[nx][ny] = true;
pq.emplace(min(val, grid[nx][ny]), nx, ny);
}
}
}
return -1;
}
};
import java.util.*;
class Solution {
public int maximumMinimumPath(int[][] grid) {
int m = grid.length, n = grid[0].length;
boolean[][] visited = new boolean[m][n];
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);
pq.offer(new int[]{grid[0][0], 0, 0});
visited[0][0] = true;
int[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};
while (!pq.isEmpty()) {
int[] curr = pq.poll();
int val = curr[0], x = curr[1], y = curr[2];
if (x == m-1 && y == n-1) return val;
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny]) {
visited[nx][ny] = true;
pq.offer(new int[]{Math.min(val, grid[nx][ny]), nx, ny});
}
}
}
return -1;
}
}
class PriorityQueue {
constructor() {
this.data = [];
}
push(item) {
this.data.push(item);
this.data.sort((a, b) => b[0] - a[0]);
}
pop() {
return this.data.shift();
}
isEmpty() {
return this.data.length === 0;
}
}
var maximumMinimumPath = function(grid) {
let m = grid.length, n = grid[0].length;
let visited = Array.from({length: m}, () => Array(n).fill(false));
let pq = new PriorityQueue();
pq.push([grid[0][0], 0, 0]);
visited[0][0] = true;
let dirs = [[0,1],[1,0],[0,-1],[-1,0]];
while (!pq.isEmpty()) {
let [val, x, y] = pq.pop();
if (x === m-1 && y === n-1) return val;
for (let [dx, dy] of dirs) {
let nx = x + dx, ny = y + dy;
if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny]) {
visited[nx][ny] = true;
pq.push([Math.min(val, grid[nx][ny]), nx, ny]);
}
}
}
return -1;
};
grid
, you start at the top-left cell (0, 0)
and want to reach the bottom-right cell (m-1, n-1)
by moving only up, down, left, or right. Each cell contains a value. The score of a path is the minimum value encountered along that path. Your task is to find a path from the start to the end such that this minimum value is as large as possible (i.e., maximize the minimum value along the path). Each cell can be used at most once in the path.
Consider the grid:
grid = [ [5, 4, 5], [1, 2, 6], [7, 4, 6] ]
O(2^{m+n})
due to all possible paths, which is not feasible for large grids.
O(mn \log(mn))
because each cell is pushed into the heap at most once, and heap operations take O(\log(mn))
.O(mn)
for the visited matrix and the heap.