跳到主要内容

Minimum Path Sum

描述

Given a m × n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time

分析

跟第 ??? 节 Unique Paths 很类似。

设状态为f[i][j],表示从起点(0,0)到达(i,j)的最小路径和,则状态转移方程为:

f[i][j]=min(f[i-1][j], f[i][j-1])+grid[i][j]

备忘录法

# Minimum Path Sum
# 备忘录法
class Solution:
def minPathSum(self, grid: list[list[int]]) -> int:
m = len(grid)
n = len(grid[0])
self.f = [[-1] * n for _ in range(m)]
return self.dfs(grid, m-1, n-1)

def dfs(self, grid: list[list[int]], x: int, y: int) -> int:
if x < 0 or y < 0:
return float('inf') # 越界,终止条件,注意,不是0

if x == 0 and y == 0:
return grid[0][0] # 回到起点,收敛条件

return min(self.getOrUpdate(grid, x - 1, y),
self.getOrUpdate(grid, x, y - 1)) + grid[x][y]

def getOrUpdate(self, grid: list[list[int]], x: int, y: int) -> int:
if x < 0 or y < 0:
return float('inf') # 越界,注意,不是0
if self.f[x][y] >= 0:
return self.f[x][y]
else:
self.f[x][y] = self.dfs(grid, x, y)
return self.f[x][y]

动规

// Minimum Path Sum
// 二维动规
public class Solution {
public int minPathSum(int[][] grid) {
final int m = grid.length;
final int n = grid[0].length;
if (m == 0) return 0;

int[][] f = new int[m][n];
f[0][0] = grid[0][0];
for (int i = 1; i < m; i++) {
f[i][0] = f[i - 1][0] + grid[i][0];
}
for (int i = 1; i < n; i++) {
f[0][i] = f[0][i - 1] + grid[0][i];
}

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
f[i][j] = Math.min(f[i - 1][j], f[i][j - 1]) + grid[i][j];
}
}
return f[m - 1][n - 1];
}
}

动规+滚动数组

// Minimum Path Sum
// 二维动规+滚动数组
public class Solution {
public int minPathSum(int[][] grid) {
final int m = grid.length;
final int n = grid[0].length;

int[] f = new int[n];
Arrays.fill(f, Integer.MAX_VALUE); // 初始值是 INT_MAX,因为后面用了min函数。
f[0] = 0;

for (int i = 0; i < m; i++) {
f[0] += grid[i][0];
for (int j = 1; j < n; j++) {
// 左边的f[j],表示更新后的f[j],与公式中的f[i[[j]对应
// 右边的f[j],表示老的f[j],与公式中的f[i-1][j]对应
f[j] = Math.min(f[j - 1], f[j]) + grid[i][j];
}
}
return f[n - 1];
}
}

相关题目