Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Advanced Graphs

Path with Minimum Effort

MEDIUM✓ CHIP-TIMEDLC #1631 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Walk a grid from the top-left cell to the bottom-right one, where each step costs the absolute height difference to the next cell. Minimize the worst single step on the route — not the total climbed.

A grid of elevation values arrives, and the walk goes from the top-left cell to the bottom-right one, moving only up, down, left, or right one cell at a time.

Each step between two cells costs the absolute difference in their elevations. A route's overall cost isn't the sum of those step costs — it's just the single largest step anywhere along the way.

The task is to pick a route, among all the ones that reach the bottom-right corner, whose worst single step is as small as possible. Multiple routes can tie on that worst-step value.

  • grid can span roughly a couple hundred rows and columns
  • elevations are non-negative integers, can repeat
  • movement is only the four orthogonal directions, no diagonals
  • route cost is the maximum single-step difference, not a running sum
  • answer is that minimized maximum step value
EX 01
heights = [[5]]
→ 0
single cell, no movement needed
EX 02
heights = [[1, 10]]
→ 9
two cells, one edge
EX 03
heights = [[3], [8]]
→ 5
vertical pair

The hintstake only what you need

HINT 1 THE NUDGE

A route's cost isn't a sum of its steps — it's just the single worst step along the way. That changes what "shortest" means before any algorithm gets involved.

HINT 2 THE STRUCTURE

This is still shortest-path, but the edge weight of a step folds into the running cost with max, not addition. Anything that generalizes Dijkstra to a different combine operator will work.

HINT 3 ONE STEP FROM THE ANSWER

Run Dijkstra from the start cell. Relax with newEffort = max(currentEffort, heightDiff) instead of currentEffort + heightDiff, and stop the moment the goal cell is popped off the heap.

Coach's boardthe pattern, step by step

THE GENTLEST CLIMBPATTERN · DIJKSTRA, MAX NOT SUMheights = [[1,2,3],[3,8,4],[5,3,5]]
0
STEP 1

Start the climb at (0,0), effort 0. A min-heap always pops the cheapest reachable cell next — Dijkstra, but relaxation takes the MAX step instead of a sum.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/path-with-minimum-effort.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def minimumEffortPath(self, heights: List[List[int]]) -> int:
        rows, cols = len(heights), len(heights[0])
        dist = [[float('inf')] * cols for _ in range(rows)]
        dist[0][0] = 0
        pq = [(0, 0, 0)]
        while pq:
            d, r, c = heapq.heappop(pq)
            if d > dist[r][c]:
                continue
            if r == rows - 1 and c == cols - 1:
                return d
            for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nr, nc = r + dr, c + dc
                if 0 <= nr < rows and 0 <= nc < cols:
                    nd = max(d, abs(heights[nr][nc] - heights[r][c]))
                    if nd < dist[nr][nc]:
                        dist[nr][nc] = nd
                        heapq.heappush(pq, (nd, nr, nc))
        return 0
TIME O(ROWS·COLS·LOG(ROWS·COLS))SPACE O(ROWS·COLS)PYTHON · RACE PACE · 20 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED