Graph Theory37 sections · 1633 units
Open in Course

LeetCode 1631 Path with Minimum Effort - Algorithm

Modified Dijkstra for minimax

For each cell, track the minimum effort needed to reach it. Effort here means the maximum single step on the path. When you pop cell (r, c) with effort e, you check neighbors. For neighbor (nr, nc), calculate the step effort abs(height[r][c] - height[nr][nc]).

The path effort to reach (nr, nc) via this path is max(e, step_effort). If that's better than the current best for (nr, nc), update it and push (max(e, step_effort), nr, nc) onto the heap.

This runs in O((V+E)logV)O((V + E) \log V) time and uses O(V)O(V) space.