题目:

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. MATLAB实现最优低通滤波器的函数

    MATLAB实现最优低通滤波器的函数 % Fs     --Data rate % Fpass  --pass band % Fstop  --Cutoff frequencies % Apass  ...

  2. ILA用法

    Ila在使用过程中Capture mode可选, write_hw_ila_data 把从ILA中读出的数据写入文件中. Syntax write_hw_ila_data [-force] [-csv ...

  3. 20170906工作日记--volley源码的相关方法细节学习

    1. 在StringRequest类中的75行--new String();使用方法 /** * 工作线程将会调用这个方法 * @param response Response from the ne ...

  4. Shell编程-06-Shell中的if语句

    目录 基本语法 if示例     在任何一门语言中,判断语句总是少不了,今天来学习一下Shell中的if语句. 基本语法 单分支情况 第一种语法 if <条件表达式> then 语句 fi ...

  5. HDU1459 非常可乐(BFS) 2016-07-24 15:00 165人阅读 评论(0) 收藏

    非常可乐 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...

  6. codevs 1083

    这道题是看了人家大牛的解题报告: 对了,要说明一下,(A+B)&1 ,表示,判断(A+B)是奇数否? 下面给出代码: #include<iostream> #include< ...

  7. Redis.conf配置文件内容详解

    #默认以后台方式运行 daemonize yes #指定redis pid文件 pidfile /data/apps/var/redis2/redis2.pid #指定redis启动占用的端口 por ...

  8. Nodejs书写爬虫工具

    看了几天的nodejs,的确是好用,全当是练手了,就写了一个爬虫工具. 爬虫思路都是一致的,先抓取页面数据,然后分析页面,获取到所需要的数据,最后获得这些数据,是写入到硬盘,还是显示到网页,自己看着办 ...

  9. JS——图片预览功能

    <script type="text/javascript">    function DisplayImage(fileTag) {        document. ...

  10. webapi 用腾讯云手机短信验证码

    #region 验证码相关 public class SmsSDK { #region 短信发送的必要参数 /// <summary> /// appId /// </summary ...