The problem asks to find the maximum score of a path from the top-left corner (0,0) to the bottom-right corner (m-1, n-1) in a given grid. The score of a path is defined as the minimum value encountered along that path. This means we need to find the path where the smallest element is as large as possible.
The optimal approach is to use a combination of binary search and union-find.
1. Binary Search:
We can perform binary search on the range of possible minimum values in the grid (from 0 to the maximum value in the grid). For a given threshold
, we check if there exists a path from (0,0) to (m-1, n-1) where all values along the path are greater than or equal to threshold
. If such a path exists, it means we might be able to find a path with an even higher minimum value. Otherwise, we need to lower the threshold
.
2. Union-Find:
To efficiently check the existence of a path with a minimum value greater than or equal to threshold
, we use a union-find data structure.
m*n
elements, one for each cell in the grid.threshold
, we union it with its adjacent cells that also have values above the threshold
.threshold
.Time Complexity Analysis:
max_value
is the maximum value in the grid.Space Complexity Analysis:
The space complexity is dominated by the union-find data structure and the grid itself, which is O(m*n).
Code Examples (Python):
import itertools
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, i):
if self.parent[i] == i:
return i
self.parent[i] = self.find(self.parent[i])
return self.parent[i]
def union(self, i, j):
root_i = self.find(i)
root_j = self.find(j)
if root_i != root_j:
if self.rank[root_i] < self.rank[root_j]:
self.parent[root_i] = root_j
elif self.rank[root_i] > self.rank[root_j]:
self.parent[root_j] = root_i
else:
self.parent[root_j] = root_i
self.rank[root_i] += 1
return True
return False
def maximumMinimumPath(grid):
m, n = len(grid), len(grid[0])
cells = sorted([(grid[i][j], i, j) for i, j in itertools.product(range(m), range(n))], reverse=True)
low, high = 0, max(max(row) for row in grid)
ans = 0
while low <= high:
mid = (low + high) // 2
uf = UnionFind(m * n)
for val, i, j in cells:
if val >= mid:
for x, y in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
if 0 <= x < m and 0 <= y < n and grid[x][y] >= mid:
uf.union(i * n + j, x * n + y)
if uf.find(0) == uf.find(m * n - 1):
ans = mid
low = mid + 1
else:
high = mid - 1
return ans
This Python code efficiently solves the problem using binary search and union-find, providing a clear and concise solution with optimal time complexity. Other languages (Java, C++, Go, TypeScript, Rust) would follow similar algorithmic structures, although the specific syntax and data structures might vary.