Path With Minimum Effort

Given an m x n grid of heights, move in four directions from the top-left cell to the bottom-right cell. The effort of a path is the maximum absolute difference between adjacent cells along that path. Return the minimum possible effort. Pattern focus: Prim-style graph traversal on a grid where the path cost is the maximum edge weight seen so far.

Input Format

heights = grid of non-negative integers

Output Format

minimum effort needed to travel from top-left to bottom-right

Constraints

  • 1 <= m, n <= 100
  • 1 <= m*n <= 10^4
  • 0 <= heights[i][j] <= 10^6

Examples

Example 1:

Input:

heights = [[1,2,2],[3,8,2],[5,3,5]]

Output:

2

Explanation:

One optimal route keeps the maximum step difference at 2.

Example 2:

Input:

heights = [[1,2,3],[3,8,4],[5,3,5]]

Output:

1

Explanation:

A path exists where every adjacent height difference is at most 1.

Example 3:

Input:

heights = [[7]]

Output:

0

Explanation:

With a single cell, no movement is needed, so the effort is 0.

Loading...
Path With Minimum Effort - Advanced Graphs