给定一个包含非负整数的 m x n
网格 grid
,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
输入:
grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:
7
解释:
因为路径 1→3→1→1→1 的总和最小。
输入:
grid = [[1,2,3],[4,5,6]]
输出:
12
impl Solution {
pub fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let (rows, cols) = (grid.len(), grid[0].len());
let mut dp = vec![0; cols];
dp[0] = grid[0][0];
(1..cols).for_each(|c| {
dp[c] = dp[c - 1] + grid[0][c];
});
(1..rows).for_each(|r| {
dp[0] += grid[r][0];
(1..cols).for_each(|c| {
dp[c] = dp[c].min(dp[c - 1]) + grid[r][c];
});
});
return dp[cols - 1];
}
}
func minPathSum(grid [][]int) int {
rows, cols := len(grid), len(grid[0])
dp := make([]int, cols)
dp[0] = grid[0][0]
for c := 1; c < cols; c++ {
dp[c] = dp[c-1] + grid[0][c]
}
for r := 1; r < rows; r++ {
dp[0] += grid[r][0]
for c := 1; c < cols; c++ {
if dp[c-1] < dp[c] {
dp[c] = dp[c-1] + grid[r][c]
} else {
dp[c] += grid[r][c]
}
}
}
return dp[cols-1]
}
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
const int rows = grid.size(), cols = grid[0].size();
int dp[cols];
dp[0] = grid[0][0];
for (int c = 1; c < cols; ++c) {
dp[c] = dp[c - 1] + grid[0][c];
}
for (int r = 1; r < rows; ++r) {
dp[0] += grid[r][0];
for (int c = 1; c < cols; ++c) {
dp[c] = min(dp[c], dp[c - 1]) + grid[r][c];
}
}
return dp[cols - 1];
}
};
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
dp = [0] * cols
for c in range(cols):
dp[c] = dp[c - 1] + grid[0][c]
for r in range(1, rows):
dp[0] += grid[r][0]
for c in range(1, cols):
dp[c] = min(dp[c], dp[c - 1]) + grid[r][c]
return dp[cols - 1]
class Solution {
public int minPathSum(int[][] grid) {
final int rows = grid.length, cols = grid[0].length;
final int[] dp = new int[cols];
dp[0] = grid[0][0];
for (int c = 1; c < cols; ++c) {
dp[c] = dp[c - 1] + grid[0][c];
}
for (int r = 1; r < rows; ++r) {
dp[0] += grid[r][0];
for (int c = 1; c < cols; ++c) {
dp[c] = Math.min(dp[c], dp[c - 1]) + grid[r][c];
}
}
return dp[cols - 1];
}
}
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】三连走一波~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~