快轉到主要內容
  1. LeetCode/

LeetCode 1631: Path With Minimum Effort

·2 分鐘· ·
LeetCode Medium Graph Dijkstra Shortest-Path
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2026-04-25 來源:Day 10 learning note

學習脈絡
#

這篇是從 learning note 裡該 LeetCode 題目的段落重新整理出來的版本。我保留當天筆記中的修正點、比較點、容易犯錯的地方,並移除同一天其他非 LeetCode 主題,避免文章內容混題。

筆記中提到的相關提醒
#

    1. Explain why LC 1631 is still Dijkstra even though the path cost is not a sum.

當天筆記摘錄
#

Problem 3 - LC 1631 Path With Minimum Effort Review
#

  • Status: Good enough after wording repair.
  • Pattern: Dijkstra on a grid with non-sum path cost.

Why Dijkstra Still Works
#

The path cost is not the sum of edge weights.

Instead:

new_effort = max(current_effort, abs(height_diff))

That means the path effort is:

non-decreasing as the path extends

not strictly increasing.

That monotonic property is why Dijkstra still works.

Heap State
#

(effort, row, col)

Transition
#

For each neighbor:

new_effort = max(current_effort, abs(heights[r][c] - heights[nr][nc]))

Finalization Rule
#

when a cell is popped from the min-heap for the first time, its minimum effort is finalized

Complexity
#

Time: O(R * C * log(R * C))
Space: O(R * C)

Common Mistakes
#

  • Do not say the effort strictly increases.
  • Do not say time is just O(R * C); heap operations add a log factor.
  • Do not say a public key decrypts a signature in the TLS analogy. That was a separate wording issue from the topic block.

正確解法
#

上面的筆記保留了推理脈絡和當天需要修正的點。下面是我會提交的版本。

from heapq import heappop, heappush
from typing import List

class Solution:
    def minimumEffortPath(self, heights: List[List[int]]) -> int:
        m, n = len(heights), len(heights[0])
        dist = [[float('inf')] * n for _ in range(m)]
        dist[0][0] = 0
        heap = [(0, 0, 0)]
        dirs = [(1,0), (-1,0), (0,1), (0,-1)]

        while heap:
            effort, r, c = heappop(heap)
            if (r, c) == (m - 1, n - 1):
                return effort
            if effort != dist[r][c]:
                continue
            for dr, dc in dirs:
                nr, nc = r + dr, c + dc
                if 0 <= nr < m and 0 <= nc < n:
                    ne = max(effort, abs(heights[r][c] - heights[nr][nc]))
                    if ne < dist[nr][nc]:
                        dist[nr][nc] = ne
                        heappush(heap, (ne, nr, nc))
        return 0

複雜度
#

Time O(mn log(mn)), Space O(mn).

要特別避免的錯誤
#

  • Summing edge weights instead of taking max.
  • Using plain BFS despite weighted efforts.

面試口說整理
#

先講清楚 state definition,再說 transition 為什麼維持這個 state。只要這題有 loop direction、狀態壓縮、或題型相似但 answer shape 不同的地方,就要主動講出來,因為那通常就是這類題最容易出錯的點。

相關文章

LeetCode 787: Cheapest Flights Within K Stops
·2 分鐘
LeetCode Medium Graph Bellman-Ford Shortest-Path
LeetCode 787 解題筆記,依照原始 learning note 重新整理
LeetCode 778: Swim in Rising Water
·2 分鐘
LeetCode Hard Graph Dijkstra Binary-Search
LeetCode 778 解題筆記,依照原始 learning note 重新整理
LeetCode 802: Find Eventual Safe States
·2 分鐘
LeetCode Medium Graph Topological-Sort
LeetCode 802 解題筆記,依照原始 learning note 重新整理
LeetCode 851: Loud and Rich
·2 分鐘
LeetCode Medium Graph Topological-Sort
LeetCode 851 解題筆記,依照原始 learning note 重新整理
LeetCode 2115: Find All Possible Recipes from Given Supplies
·2 分鐘
LeetCode Medium Graph Topological-Sort
LeetCode 2115 解題筆記,依照原始 learning note 重新整理
LeetCode 310: Minimum Height Trees
·2 分鐘
LeetCode Medium Graph Topological-Sort
LeetCode 310 解題筆記,依照原始 learning note 重新整理