作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/minimum-path-sum/description/

题目描述

Given a m x 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.

Example:

Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

题目大意

求一个矩阵从左上角到右下角的最短路径和。

解题方法

第一感觉是dfs,但是题目没有说范围,估计会超时。然后就想到了DP。

使用DP创建了一个path数组,和grid数组是一样的。path代表了从左上角开始到某个点的最短路径。那么很容易知道,新的一个点的最短路径一定等于其上方、左方最短路径+当前的值。因此写成双重循环即可。因为要用到上方、左方的值,数组第一行和第一列会超出边框,其实只需要把这个方向设为前面的那个路径值即可。

这个算法的时间啊复杂度是O(m * n),空间复杂度是O(m * n)。

代码如下:

class Solution:
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid or not grid[0]: return 0
m, n = len(grid), len(grid[0])
path = copy.deepcopy(grid)
for i in range(m):
for j in range(n):
if i == 0 and j == 0:
before = 0
elif i == 0:
before = path[i][j-1]
elif j == 0:
before = path[i-1][j]
else:
before = min(path[i-1][j], path[i][j-1])
path[i][j] = before + grid[i][j]
return path[m-1][n-1]

发现path数组没有必要重新复制出来,可以直接使用grid代表了。这样实际上就对grid进行了一个覆盖:遍历过的地方代表path,还没遍历到的地方代表grid。

这个算法的时间复杂度是O(m * n),空间复杂度是O(1)。由于少了复制数组的一步,事实上真的变快了。

class Solution:
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid or not grid[0]: return 0
m, n = len(grid), len(grid[0])
for i in range(m):
for j in range(n):
if i == 0 and j == 0:
before = 0
elif i == 0:
before = grid[i][j-1]
elif j == 0:
before = grid[i-1][j]
else:
before = min(grid[i-1][j], grid[i][j-1])
grid[i][j] = before + grid[i][j]
return grid[m-1][n-1]

二刷的时候使用的C++,方法仍然是动态规划,第一行的每个状态等于左边状态+当前位置,和第一列的每个状态等于上边状态+当前位置。其余位置等于上边和左边的状态最小值加上当前位置。

C++代码如下:

class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
const int M = grid.size(), N = grid[0].size();
vector<vector<int>> dp(M, vector<int>(N, 0));
dp[0][0] = grid[0][0];
for (int i = 1; i < M; ++i)
dp[i][0] = dp[i - 1][0] + grid[i][0];
for (int j = 0; j < N; ++j)
dp[0][j] = dp[0][j - 1] + grid[0][j];
for (int i = 1; i < M; ++i) {
for (int j = 1; j < N; ++j) {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
return dp[M - 1][N - 1];
}
};

参考资料:

日期

2018 年 9 月 11 日 —— 天好阴啊
2018 年 12 月 29 日 —— 2018年剩余电量不足1%

【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)的更多相关文章

  1. LeetCode: Minimum Path Sum 解题报告

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  2. leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. [LeetCode] 64. Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  4. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  6. C#解leetcode 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. [leetcode]64. Minimum Path Sum最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. [leetcode] 64. Minimum Path Sum (medium)

    原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...

  9. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

随机推荐

  1. Shell中 ##%% 操作变量名

    在linxu平台下少不了对变量名的处理,今天记录下shell中 ##%% 对变量名的操作. #操作左侧,%操作右侧. #号处理方式: 对于单个#,处理对象为变量中指定的第一个符号左侧字符串, 对于两个 ...

  2. Perl字符串处理函数用法集锦

    Perl字符串处理函数 0.函数名 index 调用语法position=index(string,substring,position); 解说返回子串substring在字符串string中的位置 ...

  3. 每日自动健康打卡(Python+腾讯云服务器)

    每日自动健康打卡(Python+腾讯云服务器) 1.配置需要 python3.7,Chrome或者Edeg浏览器,Chrome驱动或者Edge驱动 #需要配置selenium库,baidu-aip库, ...

  4. 简单的Mybatis程序

    1.新建一个普通Maven项目,导入 mybatis.mysql.junit(用于测试)3个依赖 Mybatis <dependency> <groupId>org.mybat ...

  5. acupuncture

    acute+puncture. [woninstitute.edu稻糠亩] To understand the basics of acupuncture, it is best to familia ...

  6. 零基础学习java------25--------jdbc

    jdbc开发步骤图 以下要用到的products表 一. JDBC简介 补充    JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口,各个数据库厂商趋势线这个接口,提 ...

  7. Angular中怎样创建service服务来实现组件之间调用公共方法

    Angular组件之间不能互相调用方法,但是可以通过创建服务来实现公共方法的调用. 实现 创建服务命令 ng g service 服务路径/服务名 比如这里在app/services目录下创建stor ...

  8. 案例分析 CAN OPEN 调试记录 进度

    2020.12.29 发现一片博客:https://blog.csdn.net/harrycomeon/article/details/94650103 需要一个硬件:CAN分析仪,网上200元左右. ...

  9. C++异常处理(try、catch、throw)

    本文为转载 博主原文连接 我们通常希望自己编写的程序能够在异常的情况下也能作出相应的处理,而不至于程序莫名其妙地中断或者中止运行了.在设计程序时应充分考虑各种异常情况,并加以处理. 在C++中,一个函 ...

  10. Shell学习(四)——shell中各种括号的作用

    参考博客: [1]shell中各种括号的作用().(()).[].[[]].{} [2]shell中的单层大/中/小括号.双层大中小括号.命令替换等 一.前言 目录 单括号() 双括号(( )) 单中 ...