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. Logstash之时区问题的建议和修改---filter---and duplicate resolution.

    2. logstash es duplicate https://logstash.jira.com/browse/LOGSTASH-1875 https://logstash.jira.com/br ...

  2. python console

    print(sys.stdout.encoding, locale.getpreferredencoding ()) windows console : chcp 65001; 在设置了这个环境变量时 ...

  3. LeetCode_Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. vxworks 的 socket, thread, 信号量模型

    http://www.vxdev.com/docs/vx55man/vxworks/netguide/c-sockets.html http://www.vxdev.com/docs/vx55man/ ...

  5. EXPORT_SYMBOL使用

    EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static 函数和变量都会自动导入到kernel 空间的, 都不用EXPORT_SYMBOL() 做标记的.2.6就必须用EXPO ...

  6. jetBrains设置appium环境

  7. ubuntu apt-get 安装 lnmp

    最近在 Ubuntu 14.04 LTS 安装 LNMP 一键安装包的时候出现了问题,PHP 5 服务没有启动,只好使用 Ubuntu 官方源进行安装: Nginx (读音 “engine x”)免费 ...

  8. scp无密码访问scp -i

    网上搜索,可以发现大概有以下情况 1.使用expect交互 2.sshpass 但因为他们都需要安装第三方的软件,不适合我们这样的场景,我们很多时间是绝不允许安装其他软件在客户机上的. 我的场景是在本 ...

  9. leveldb0

    leveldb的源代码进行学习,则纯粹是出于一个码农对美好世界进行探究的好奇.接下来将尽可能从源代码上给出leveldb代码的详尽注释,这里先列出自己在阅读前后的主要参考. 0 官方文档http:// ...

  10. 无密码ssh操作步骤备忘

    需求:A机器无密码登陆到B机器 1.A机器执行   ssh-keygen -t rsa  ,在~/.ssh/下生成id_rsa 和  id_rsa.pub两个文件,其中id_rsa.pub是公匙 2. ...