LeetCode-Minimum Path Sum[dp]
Minimum Path Sum
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.
分析:
动态规划的经典题目,设dp[i][j]表示从位置[0,0]到[i,j]的最小路径和,要到达位置[i,j]只能从[i,j-1]或[i-1,j]向右或向下走一步到达,
所以状态转移方程为:
dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];
由二维dp进一步优化为一维dp:
当j=0时,dp[j]=dp[j]+grid[i][j];
当0<j&&j<n时,dp[j]=min(dp[j],dp[j-1])+grid[i][j];
(此时的dp[j]等价于二维dp中的dp[i][j])
参考代码:
public class Solution {
public int minPathSum(int[][] grid) {
int nlen=grid.length;
int mlen=grid[0].length;
int dp[]=new int[mlen];
dp[0]=grid[0][0];
for(int j=1;j<mlen;j++){
dp[j]=dp[j-1]+grid[0][j];
}
for(int i=1;i<nlen;i++){
for(int j=0;j<mlen;j++){
dp[j]=grid[i][j]+(j==0?(dp[j]):(Math.min(dp[j-1], dp[j])));
}
}
return dp[mlen-1];
}
}
LeetCode-Minimum Path Sum[dp]的更多相关文章
- 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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- LeetCode Minimum Path Sum (简单DP)
题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...
- [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 ...
- 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 ...
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- LeetCode:Minimum Path Sum(网格最大路径和)
题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...
- [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 ...
- Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)
Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...
- 【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 ...
随机推荐
- python之路第一篇
一.python环境的搭建 1.window下环境的搭建 (1).在 https://www.python.org/downloads/ 下载自己系统所需要的python版本 (2).安装python ...
- java知识点整理
1 java 和Tomcat总结 脑图地址 (其中web 容器部分还需要继续完善,但是没找到相关文档) 跟着java Se 文档梳理了一下学习路线图(方便全面掌握要点,及时对自己查漏补缺),以及一些 ...
- Eclipse 下的 Maven的安装及配置
http://jingyan.baidu.com/article/295430f136e8e00c7e0050b9.html
- Spring mvc 中使用 kaptcha 验证码
生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的开源项目.使用Kaptcha 生成验证码十分简单并且参数可以进行自定义.只需添加jar包配置下就可 ...
- Tomcat、JBOSS、WebSphere、WebLogic、Apache等技术概述
Tomcat:应用也算非常广泛的web服务器,支持部分j2ee,免费,出自apache基金组织 JBoss:开源的应用服务器,比较受人喜爱,免费(文档要收费) Weblogic:应该说算是业界 ...
- 使用Swagger实现webapi接口自动化文档生成
这里是实现自动化api稳当的生成,在网上看了很多swagger的文档,可能都是在为实现接口时直接使用的swagger,其实步骤差不多,但是更加详细的我还没看到,又或者说,我看着文档来的时候还是出错啦, ...
- jquery 检测某元素是否含有某属性
检测某元素是否含有某属性 if(typeof($("#aid").attr("rel"))=="undefined")
- js中各个类型的转换总结
字符串转换为数组: 1 正则表达式var string=“abcdedef”var obj=string.replace(/(.)(?=[^$])/g,"$1,").split ...
- Android各种Manager
一.PowerManager 主要是用来控制电源状态,设置屏幕状态,和电池待机状态 PowerManager pm = ((PowerManager)getSystemService(POWER_S ...
- 配置一个完整的 applicacontext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...