【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 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
随机推荐
- nginx负均
Nginx负载均衡详解 上一篇中我说啦nginx有哪些中负载均衡算法.这一结我就给如果操作配置的给大家做详细说明下. 首先给大家说下upstream这个配置的,这个配置是写一组被代理的服务器地址,然后 ...
- GORM基本使用
GORM 目录 GORM 1. 安装 2. 数据库连接 3. 数据库迁移及表操作 1. 安装 go get -u github.com/jinzhu/gorm 要连接数据库首先要导入驱动程序 // G ...
- linux命令行快速统计文件(压缩文件)的行数
统计(文件|压缩文件)的行数 zcat file.gz | sed -n '$=' #迅速.直接打印出多少行.-n 取消 ...
- Linux学习 - 使用qq邮箱发送邮件
1 打开qq邮箱,设置->账户->POP3/SMTP,开启服务 2 配置/etc/mail.rc文件 set from=73***32@qq.com #设置发送方邮件地址 set smtp ...
- jdk1.6,1.7,1.8解压版无需安装(64位)
1.java SE 1.6各个版本 jdk http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads ...
- jenkins+Gitlab安装及初步使用
安装包下载地址:https://packages.gitlab.com/gitlab/gitlab gitlab-cerpm 包国内下载地址: https://mirrors.tuna.tsinghu ...
- 修改页面.JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@tag ...
- python3.6.4 scrapy框架from PIL import Image报错 from . import _imaging as core
scrapy框架爬取url下载图片时,用ImagesPipeline下载图片 from PIL import Image报错 from . import _imaging as core Import ...
- SpringCloud技术涵盖简介
SpringCloud是微服务架构的集大成者,云计算最佳业务实践. 我们平常使用的Spring和他们的关系,对Spring,springboot , SpringCloud 的 概念区分,上图: Sp ...
- .NET静态代码织入——肉夹馍(Rougamo)
肉夹馍是什么 肉夹馍通过静态代码织入方式实现AOP的组件..NET常用的AOP有Castle DynamicProxy.AspectCore等,以上两种AOP组件都是通过运行时生成一个代理类执行AOP ...