【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛
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++)的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- [leetcode] 64. Minimum Path Sum (medium)
原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
随机推荐
- 【系统硬件】英伟达安培卡 vs 老推理卡硬件参数对比
欢迎关注我的公众号 [极智视界],回复001获取Google编程规范 O_o >_< o_O O_o ~_~ o_O 本文分享一下英伟达安培卡 vs 老推理 ...
- 多选项、多个选择项【c#】
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddDataInfoCe ...
- binlog真的是银弹吗?有些时候也让人头疼
大家好,我是架构摆渡人.这是实践经验系列的第三篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果有收获,还请分享给更多的朋友. binlog 用于记录用户对数据库操作的SQL语句信息,同时主 ...
- 用前端表格技术构建医疗SaaS 解决方案
电子健康档案(Electronic Health Records, EHR)是将患者在所有医疗机构产生的数据(病历.心电图.医疗影像等)以电子化的方式存储,通过在不同的医疗机构之间共享,让患者面对不同 ...
- 【leetcode】633. Sum of Square Numbers(two-sum 变形)
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...
- 【bfs】洛谷 P1443 马的遍历
题目:P1443 马的遍历 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 记录一下第一道ac的bfs,原理是利用队列queue记录下一层的所有点,然后一层一层遍历: 其中: 1.p ...
- 100个Shell脚本——【脚本9】统计ip
[脚本9]统计ip 有一个日志文件,日志片段:如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com "/ ...
- Linux学习 - 文件系统常用命令
一.文件系统查看命令df df [选项] [挂载点] -a 查看所有文件系统信息,包括特殊文件系统 -h 使用习惯单位显示容量 -T 显示文件系统类型 -m 以MB为单位显示容量 -k 以KB为单位显 ...
- Undefined symbols for architecture arm64:问题
Undefined symbols for architecture arm64: "_sqlite3_prepare_v2", referenced from: +[HMJSch ...
- Linux基础命令---htdigest建立和更新apache服务器摘要
htdigest htdigest指令用来建立和更新apache服务器用于摘要认证的存放用户认证信息的文件. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS. 1.语法 ...