题目:

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.

思路:

设res[i][j]表示从左上角到grid[i][j]的最小路径和。那么res[i][j] = grid[i][j] + min( res[i-1][j], res[i][j-1] );

下面的代码中,为了处理计算第一行和第一列的边界条件,我们令res[i][j]表示从左上角到grid[i-1][j-1]的最小路径和,最后rs[m][n]是我们所求的结果。

/**
* @param {number[][]} grid
* @return {number}
*/
var minPathSum = function(grid) {
var m=grid.length,n;
if(m==0){
return 0;
}else{
n=grid[0].length;
} var res=[];
for(var i=0;i<m;i++){
for(var j=0;j<n;j++){
res[i]=[];
}
} for(var i=0;i<m;i++){
for(var j=0;j<n;j++){
if(i==0&&j==0)res[0][0]=grid[0][0];
else if(j==0)res[i][0]=res[i-1][0]+grid[i][0];
else if(i==0)res[0][j]=res[0][j-1]+grid[0][j];
else res[i][j]=Math.min(res[i-1][j],res[i][j-1])+grid[i][j]; }
} return res[m-1][n-1];
};

【数组】Minimum Path Sum的更多相关文章

  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. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  3. 【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 ...

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

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

  5. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

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

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

  8. 【LeetCode】64. 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 ...

  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. No cache or cacheManager properties have been set. Authorization cache cannot be obtained.

    20235 [http-bio-8080-exec-10] INFO o.a.shiro.realm.AuthorizingRealm - No cache or cacheManager prope ...

  2. console的所有用法

    http://jingyan.baidu.com/article/e75aca855c6419142edac6c1.html  参考它. console.info(), console.debug() ...

  3. Java中取两位小数

    请参考下面函数: private String getFormated(String s){        float f=Float.parseFloat(s);        java.text. ...

  4. [Ubuntu Version] 如何在terminal 查看当前 ubuntu的版本号

    命令: locate locate /etc/*release/etc/lsb-release/etc/os-release 命令: catcat /etc/os-releaseNAME=" ...

  5. Delphi for iOS开发指南(6):在iOS应用程序中使用ComboBox组件来从列表中选择某一项

    http://blog.csdn.net/delphiteacher/article/details/8924110 Delphi for iOS开发指南(6):在iOS应用程序中使用ComboBox ...

  6. 论文笔记(2)-Dropout-Regularization of Neural Networks using DropConnect

    这篇paper使用DropConnect来规则化神经网络.dropconnect和dropout的区别如下图所示.dropout是随机吧隐含层的输出清空,而dropconnect是input unit ...

  7. Python学习-20.Python的Urllib模块

    除了 Http 模块可以模拟 Http 请求外,使用 Urllib 模块也是可以模拟 Http 请求的,只不过功能相对弱一点. import urllib.request opener = urlli ...

  8. Ajax 访问 或 获取 IIS 虚拟目录

    使用场景 最近用 .net core mvc 写了一个工具类的项目,作为我们项目的后台管理网站使用.第一次被老大拿去部署的时候被告知不可用,同样的代码在我电脑和我的iis上都可以使用的啊. 后来才知道 ...

  9. Visual Studio Code 学习.net core初体验

    一,安装 最近在用 Visual Studio Code 学习.net core ,记录下学习的过程,首先去官网下载最新的.net core2.1安装包,有windows 和mac,根据自己的开发环境 ...

  10. MySQL事务一致性理解

    一致性是指数据处于一种语义上的有意义且正确的状态.一致性是对数据可见性的约束,保证在一个事务中的多次操作的数据中间状态对其他事务不可见的.因为这些中间状态,是一个过渡状态,与事务的开始状态和事务的结束 ...