An interactive visualization of Dijkstra's algorithm on a grid, where edge weights are absolute height differences and the goal is to minimize the maximum effort along the path from top-left to bottom-right.
Shortest Path LeetCode 1631 ↗ Mediumheights, 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 hope to travel to the bottom-right cell
(rows-1, columns-1).fun minimumEffortPath(heights: Array<IntArray>): Int { val rows = heights.size; val cols = heights[0].size val effort = Array(rows) { IntArray(cols) { Int.MAX_VALUE } } effort[0][0] = 0 val pq = PriorityQueue<Triple<Int,Int,Int>>(compareBy { it.first }) pq.add(Triple(0, 0, 0)) // (effort, row, col) while (pq.isNotEmpty()) { val (e, r, c) = pq.poll() if (r == rows - 1 && c == cols - 1) return e if (e > effort[r][c]) continue for ((dr, dc) in arrayOf(-1 to 0, 1 to 0, 0 to -1, 0 to 1)) { val nr = r + dr; val nc = c + dc if (nr in 0 until rows && nc in 0 until cols) { val newE = maxOf(e, Math.abs(heights[r][c] - heights[nr][nc])) if (newE < effort[nr][nc]) { effort[nr][nc] = newE pq.add(Triple(newE, nr, nc)) } } } } }
This problem is a shortest-path problem on a grid, but with a twist: instead of summing edge weights, we want to minimize the maximum absolute height difference along the path. We can solve this using Dijkstra's algorithm, where the "distance" to each cell is the minimum possible maximum effort to reach it. The priority queue always processes the cell with the smallest effort first, guaranteeing optimality.
effort[0][0] = 0 and push (0, 0, 0) onto the priority queue.(e, r, c) with the smallest effort from the priority queue.(r, c) is the destination (rows-1, cols-1), return e as the answer.e > effort[r][c], this entry is outdated — skip it.new_e = max(e, |heights[r][c] - heights[nr][nc]|). If new_e < effort[nr][nc], update and push onto the queue.
Each cell can be pushed to the priority queue at most 4 times (once per neighbor), and each heap
operation costs O(log(mn)). The effort array uses O(mn) space.