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 minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
题意从左上到右下,所有可能的路径中,求经过的元素和最小值。
动态规划基础题了,dp每个状态由左边或者上边的值中,较小的值与当前状态的值相加得到。
注意考虑边界情况就行了。
int dp[1000][1000];
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int rows=grid.size();
if(rows==0)return 0;
int cols=grid[0].size();
if(cols==0)return 0;
memset(dp,0,sizeof(dp));
for(int i=0;i<rows;++i)
{
for(int j=0;j<cols;++j)
{
if(i==0&&j==0)dp[0][0]=grid[0][0];
else if(j==0)dp[i][0]=dp[i-1][0]+grid[i][0];
else if(i==0)dp[0][j]=dp[0][j-1]+grid[0][j];
else dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];
}
}
return dp[rows-1][cols-1];
}
};
leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 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 解题报告
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 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(网格最大路径和)
题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...
- [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 (简单DP)
题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- [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 ...
随机推荐
- mybati之day02
今天开始讲解mybatis的第二天内容 一,拼接sql 在mapper.xml中,会多次使用到同一条sql片段,这时为了简便书写,将其定义出来 <sql id="base_sql&q ...
- 自定义控件 带描边的TextView
使用 public class MainActivity extends Activity { @Override protected void onCreate(Bundle sav ...
- (转)跟我一起学JQuery插件开发教程
在逛codeproject网站的时候,突然看到一篇文章:How to write plugin in Jquery. 如果对E文好的同学 ,可以看上面的连接.现在我把上面网站的及结合自己的想法写这篇文 ...
- [c#]asp.net开发微信公众平台(5)微信图文消息
上篇已经成功响应了关注事件,也实现了文本消息的发送,这篇开始图文消息处理, 微信中最常用的消息类型就是图文消息了,因为它图文并茂,最能表达信息. 图文消息在微信中的接口定义如下: <xml> ...
- Swift - IBOutlet返回nil(fatal error: unexpectedly found nil while unwrapping an Optional value)
在Swift 中 ViewController 默认构造方法不关联同名的xib文件 在使用OC的时候,调用ViewController的默认构造函数,会自动关联到一个与ViewController名字 ...
- 织梦DEDECMS网站首页如何实现分页翻页
织梦DEDECMS模板网站首页如何实现首页分页和翻页 方法如下:(三种方法,自己选择一种来实现分页吧) 第一种:调用ajax和参数的(不推荐)1.必须在DEDE首页模板中的<head>&l ...
- jQuery 幻灯片 ----摘录
Cloud Carousel (演示 | 下载) ShineTime (演示 | 下载) Nivo Slider (演示 | 下载) Interactive Photo Desk (演示 | 下载) ...
- DataTables列过滤器
var table = $('#example').DataTable(); table.columns().flatten().each( function ( colIdx ) { // Crea ...
- GO函数倒叙输出
package main import "fmt" func main(){ rec() } func rec(i int){ { return } rec(i+) fmt.Pri ...
- ububtu 彻底卸载程序的几种方法
sudo apt-get purge ......(点点为为程序名称) sudo apt-get autoremove sudo apt-get clean dpkg -l |grep ^rc|awk ...