Question

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.

Solution

  1. 动态规划。 p[i][j] = grid[i][j] + min(p[i - 1][j], p[i][j - 1]). 因为到达一个节点,最多有两种选择,当然是选择代价较小的。

  2. 时间复杂度O(n^2)。

Code

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
if (grid.size() <= 0)
return 0;
vector<vector<int>> tb(grid.size(), vector<int>(grid[0].size(), 0));
int row = grid.size();
int col = grid[0].size();
tb[0][0] = grid[0][0];
// 注意初始化是一个代价累加的过程
for (int i = 1; i < row; i++) {
tb[i][0] = grid[i][0] + tb[i - 1][0];
}
for (int i = 1; i < col; i++) {
tb[0][i] = grid[0][i] + tb[0][i - 1];
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < col; j++) {
tb[i][j] = grid[i][j] + min(tb[i - 1][j], tb[i][j - 1]);
}
}
return tb[row - 1][col - 1];
}
};

LeetCode——minimum-path-sum的更多相关文章

  1. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  2. 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 ...

  3. [LeetCode] 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 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]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  6. LeetCode:Minimum Path Sum(网格最大路径和)

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

  7. LeetCode Minimum Path Sum (简单DP)

    题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...

  8. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

  10. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

随机推荐

  1. 联合约束 CONCAT()

    w SELECT 原理. SELECT * FROM wz WHERE CONCAT(wint,wchar) NOT IN (SELECT CONCAT(wint,wchar) FROM wa); S ...

  2. Spark 源码分析 -- RDD

    关于RDD, 详细可以参考Spark的论文, 下面看下源码 A Resilient Distributed Dataset (RDD), the basic abstraction in Spark. ...

  3. Yii框架2.0的Gii

    Yii框架的Gii在我看来算是个快速创建器,当然对于学习来说意义不大,但对于已经懂得他的原理并用他开发的话,就是个快速开发的好工具. 他能快速的创建控制器,模块,crup,插件,Module. 打开g ...

  4. 解决 Invalid signature file digest for Manifest 问题

    idea打包的jar文件在spark执行是报错: Invalid signature file digest for Manifest 通过以下命令解决: zip -d myjob.jar META- ...

  5. Node.js REST 工具 Restify

    Restify 是一个 Node.JS 模块,可以让你创建正确的 REST web services.它借鉴了很多 express 的设计,因为它是 node.js web 应用事实上的标准 API. ...

  6. Java Code Template

    设置注释模板的入口:Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元素 ...

  7. 使用js在网页上显示时间

    <html> <script> function getDate(){ var d,s,t; d = new Date(); s = d.getFullYear().toStr ...

  8. idea配置scala和spark

    1 下载idea  路径https://www.jetbrains.com/idea/download/#section=windows 2安装spark  spark-2.1.0-bin-hadoo ...

  9. [golang note] 内建类型

    基础类型 √ golang内建基础类型有布尔类型.整数类型.浮点类型.复数类型.字符串类型.字符类型和错误类型. 复合类型 √ golang支持的复合类型有指针.数组.数组切片.字典.通道.结构体和接 ...

  10. By.Xpath快速定位页面元素常用方法

    先看一看xpath的语法 我们将在下面的例子中使用这个 XML 文档. <?xml version="1.0" encoding="ISO-8859-1" ...