LeetCode 题解工作台
网格中的最小路径代价
给你一个下标从 0 开始的整数矩阵 grid ,矩阵大小为 m x n ,由从 0 到 m * n - 1 的不同整数组成。你可以在此矩阵中,从一个单元格移动到 下一行 的任何其他单元格。如果你位于单元格 (x, y) ,且满足 x ,你可以移动到 (x + 1, 0) , (x + 1, 1) ,…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 状态·转移·动态规划
答案摘要
我们定义 表示从第一行出发,到达第 行第 列的最小路径代价。由于每次只能从上一行的某一列移动到当前行的某一列,因此 的值可以从 $f[i - 1][k]$ 转移而来,其中 的取值范围为 $[0, n - 1]$。因此状态转移方程为: $$
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个下标从 0 开始的整数矩阵 grid ,矩阵大小为 m x n ,由从 0 到 m * n - 1 的不同整数组成。你可以在此矩阵中,从一个单元格移动到 下一行 的任何其他单元格。如果你位于单元格 (x, y) ,且满足 x < m - 1 ,你可以移动到 (x + 1, 0), (x + 1, 1), ..., (x + 1, n - 1) 中的任何一个单元格。注意: 在最后一行中的单元格不能触发移动。
每次可能的移动都需要付出对应的代价,代价用一个下标从 0 开始的二维数组 moveCost 表示,该数组大小为 (m * n) x n ,其中 moveCost[i][j] 是从值为 i 的单元格移动到下一行第 j 列单元格的代价。从 grid 最后一行的单元格移动的代价可以忽略。
grid 一条路径的代价是:所有路径经过的单元格的 值之和 加上 所有移动的 代价之和 。从 第一行 任意单元格出发,返回到达 最后一行 任意单元格的最小路径代价。
示例 1:

输入:grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]] 输出:17 解释:最小代价的路径是 5 -> 0 -> 1 。 - 路径途经单元格值之和 5 + 0 + 1 = 6 。 - 从 5 移动到 0 的代价为 3 。 - 从 0 移动到 1 的代价为 8 。 路径总代价为 6 + 3 + 8 = 17 。
示例 2:
输入:grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]] 输出:6 解释: 最小代价的路径是 2 -> 3 。 - 路径途经单元格值之和 2 + 3 = 5 。 - 从 2 移动到 3 的代价为 1 。 路径总代价为 5 + 1 = 6 。
提示:
m == grid.lengthn == grid[i].length2 <= m, n <= 50grid由从0到m * n - 1的不同整数组成moveCost.length == m * nmoveCost[i].length == n1 <= moveCost[i][j] <= 100
解题思路
方法一:动态规划
我们定义 表示从第一行出发,到达第 行第 列的最小路径代价。由于每次只能从上一行的某一列移动到当前行的某一列,因此 的值可以从 转移而来,其中 的取值范围为 。因此状态转移方程为:
其中 表示从第 行第 列移动到第 行第 列的代价。
最终答案即为 。
由于每次转移只需要用到上一行的状态,因此我们可以使用滚动数组的方式,将空间复杂度优化到 。
时间复杂度 ,空间复杂度 。其中 和 分别为网格的行数和列数。
class Solution:
def minPathCost(self, grid: List[List[int]], moveCost: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
f = grid[0]
for i in range(1, m):
g = [inf] * n
for j in range(n):
for k in range(n):
g[j] = min(g[j], f[k] + moveCost[grid[i - 1][k]][j] + grid[i][j])
f = g
return min(f)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for candidates who can identify the need for dynamic programming in minimizing path costs.
- question_mark
Assess whether they understand state transitions and can apply them to this grid-based problem.
- question_mark
Evaluate if they can efficiently handle the computation and space requirements for dynamic programming.
常见陷阱
外企场景- error
Not considering all possible moves from the previous row, leading to incorrect results.
- error
Forgetting to handle edge cases, such as the last row where no further moves are possible.
- error
Incorrectly calculating move costs between cells, potentially misinterpreting the moveCost matrix.
进阶变体
外企场景- arrow_right_alt
Consider variations where the grid size or moveCost matrix dimensions are different.
- arrow_right_alt
Introduce obstacles or forbidden cells in the grid and calculate the cost accordingly.
- arrow_right_alt
Change the problem to minimize the number of moves instead of cost, introducing a different dynamic programming approach.