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 ...
随机推荐
- 禅道---Bug管理模块
禅道官网:http://www.cnezsoft.com/ 简介: 开源免费的项目管理软件.集产品管理.项目管理.测试管理一体以及事物管理组织管理的功能 使用原因: 开源 方便跟踪管理Bug 使用简单 ...
- SQL VIEW(视图)
视图是可视化的表. SQL CREATE VIEW 语句 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个 ...
- 原生javascript实现网页显示日期时钟效果
刚接触javascript中Date内置对象时,以为这些方法都太简单了,结果要自己实际操作写一个时钟效果还真一时把我难住了,主要有几点大家要注意的.先看实际效果 要实现这样的效果 某年某月某日星期几几 ...
- ADO.NET中的五大对象
Connection connection 对象主要是开启程序和数据库之间的连接.没有利用连接对象将数据库打开,是无法从数据库中取到数据的.这个物件是ADO.NET的最底层,我们可以自己产生这个对象, ...
- Android view的测量及绘制
讲真,自我感觉,我的水平真的是渣的一匹,好多东西都只停留在知道和会用的阶段,也想去研究原理和底层的实现,可是一看到代码就懵逼了,然后就看不下去了, 说自己不着急都是骗人的,我自己都不信,前两天买了本& ...
- 数组 list互转
数组 list互转 String str[] = list.toArray(new String[]{}); List list= java.util.Arrays.asList(String str ...
- Notepad++中过滤掉的正则方式
2 => 'ashadv'3 => 'aogro'4 => 'aogs'5 => 'ashamw'6 => 'arc'8 => 'gtsatq'9 => 'b ...
- curl通过调用WebService查询当前天气
<?php /** * curl通过调用WebService查询北京的当前天气 */ header("Content-type: text/html; charset=utf-8&qu ...
- ajax数据请求3(数组json格式)
ajax数据请求3(数组json格式) <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- 3.Smarty的基本语法
一.注释的方法是 {* 这里填注释 *} 二.在Smarty的输出赋值进来的变量 1.变量是字符串的时候 1)关联数组 $arr = array('a'=>'cai','b'=>'muqi ...