Path With Minimum Effort Problem
Description
LeetCode Problem 1631.
You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.
Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
Example 1:

Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
Output: 2
Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
Example 2:

Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
Output: 1
Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
Example 3:

Input: heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
Output: 0
Explanation: This route does not require any effort.
Constraints:
rows == heights.lengthcolumns == heights[i].length1 <= rows, columns <= 1001 <= heights[i][j] <= 10^6
Solution
Approach: Union-Find on Edges Sorted by Weight
Think of the grid as a graph: each cell is a node, and neighbouring cells are joined by an edge whose weight is their height difference. The effort of a route is its heaviest edge, and we want the route whose heaviest edge is as light as possible.
Add the edges in increasing order of weight, joining cells with a union-find structure. Stop at the first edge after which the top-left and bottom-right cells are in the same component; that edge's weight is the answer.
Why this works: at the moment the two corners become connected, there is a route using only edges added so far, all of weight at most the current one, so the effort is at most that weight. And before this edge was added the corners were not connected, so no route uses only lighter edges; the effort cannot be smaller.
A grid with a single cell needs no move at all, so the answer is 0; the loop never finds a connecting edge there, so return 0 after it.
Dijkstra's algorithm, with "maximum of the edges so far" in place of the sum, and a binary search on the effort with a BFS check, also solve the problem in similar time.
Complexity: O(mn log(mn)) time to sort the roughly 2mn edges, and O(mn) space.
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
m, n = len(heights), len(heights[0])
parent = list(range(m * n))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
# Every pair of neighbouring cells is an edge weighted by their height difference.
edges = []
for i in range(m):
for j in range(n):
if i + 1 < m:
edges.append((abs(heights[i][j] - heights[i + 1][j]), i * n + j, (i + 1) * n + j))
if j + 1 < n:
edges.append((abs(heights[i][j] - heights[i][j + 1]), i * n + j, i * n + j + 1))
edges.sort()
# Add edges from the cheapest; the one that first links start and end is the answer.
start, end = 0, m * n - 1
for w, a, b in edges:
parent[find(a)] = find(b)
if find(start) == find(end):
return w
return 0 # a single cell: no move needed