Minimum Path Sum leetcode java
题目:
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.
题解:
这道题跟Unique Path系列是思路一样的。具体思路看代码就很清楚了
代码如下:
1 public int minPathSum(int[][] grid) {
2 int m = grid.length;
3 int n = grid[0].length;
4
5 if(m==0||n==0)
6 return 0;
7
8 int[][] dp = new int[m][n];
9
dp[0][0]=grid[0][0];
//a row
for (int i = 1; i < n; i++)
dp[0][i] = dp[0][i - 1] + grid[0][i];
//a column
for (int j = 1; j < m; j++)
dp[j][0] = dp[j - 1][0] + grid[j][0];
for (int i=1; i<m; i++){
for (int j=1; j<n; j++){
if(dp[i-1][j]<dp[i][j-1])
dp[i][j]=dp[i-1][j]+grid[i][j];
else
dp[i][j]=dp[i][j-1]+grid[i][j];
}
}
return dp[m-1][n-1];
}
Minimum Path Sum leetcode java的更多相关文章
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Binary Tree Maximum Path Sum leetcode java
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Minimum Path Sum——LeetCode
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Path Sum leetcode java
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 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 ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
随机推荐
- UG中STP203和STP214的区别
UG转档STP203,STP214的区别:STP214转出的图档将保留原图属性,例如所在图层,曲面颜色,装配组件名称等.STP203没有上述功能.
- C++最快的读取文件的方案(scanf,cin(及取消sync),fread)的详细对比
竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式.相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据.还有人说Pascal的rea ...
- SmartSVN has inconsistent newlines解决方法
SmartSVN has inconsistent newlines解决方法 点击 Project–>Setting,选择Working copy下的EOL-style,将Default EOL ...
- 微信小程序如何玩转分销
截止目前,微信月活跃用户已经高达8.89亿,微信这个庞大的互联网巨头下一个目标是什么? 打造属于自己的“AppStore”.小程序正是完成这个微信生态体系的一块完美拼板, 张小龙预言:未来2年内,小程 ...
- SQL 版本说明
http://www.cnblogs.com/SameZhao/p/6184924.html The ProductMajorVersion产品主版本号 如: 12为 SQL SERVER 2014 ...
- LAMP学习路线图
站点开发概述 LAMP开发概述 HTML基础 CSS基础 DIV+CSS Javascript Jquery(Ajax) WAMP 环境搭建 PHP基本的语法,变量.数据类型,表达式,常量,流程控制, ...
- [Winform]setupfactory制作安装包卸载输入密码进行验证
摘要 项目有这样一个需求,在体验机上安装了一个软件,如果有用户卸载的时候,给与输入密码验证的提示,当然强制删除软件所在目录除外.那么这个有办法实现吗? 解决办法 在卸载的时候,用户单击下一步的时候进行 ...
- ES6的一些基本用法
● let ● variable hoisting ● arrow Function, Lambda表达式 ● Destructuring Assignments 解构赋值 ● 默认参数值 Defau ...
- ios nil、NULL和NSNull 的使用
nil用来给对象赋值(Objective-C中的任何对象都属于id类型),NULL则给任何指针赋值,NULL和nil不能互换,nil用于类指针赋值(在Objective-C中类是一个对象,是类的met ...
- ibatis.net:第六天,QueryForList
xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...