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 ...
随机推荐
- zend_db连接mysql(附完整代码)(转)
在看这些之前请确保你正确加载了PDO扩展. 作法是编辑php.ini手动增加下面这两行(前面要没有分号;):extension=php_pdo.dllextension=php_pdo_mysql.d ...
- 关于js中 document.body.scrollTop 不能返回正确值的原因
本来是为了通过document.body.scrollTop来获取浏览器垂直滚动条向下滚动的像素,但是不管滚动条在什么位置总是返回是0,造成这样的原因和html的头部声明有关,如果头部声明 为:< ...
- BitmapFactory 加载图片到内存
Bitmap占用内存分析 Android的虚拟机是基于寄存器的Dalvik,它的最大堆(单个进程可用内存)大小一般是16M,当然不同设备是不一样的,可以查看/system/build.prop文件,[ ...
- Java多线程练习二
public class ex3 { public static void main(String [] args) { thread2 t1 = new thread2("hello&qu ...
- 黑马程序员——HTML语言
------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...
- C#读取word模版并对指定域写入数据保存为新word
引用: using System;using System.Collections.Generic;using System.Aspose.Words;using System.Windows.For ...
- SqlCommand.Parameters.add()方法
SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间: System.Data.SqlClient 程序集: Syst ...
- sprintf()详细介绍
sprintf 编辑词条 编辑词条 --> 字串格式化命令,主要功能是把格式化的数据写入某个字符串中.sprintf 是个变参函数,使用时经常出问题,而且只要出问题通常就是能导致程序崩溃的内 ...
- Spring4分别整合mongo2.X和3.0
1.pom文件添加: <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-jav ...
- PHP数据过滤
1.php提交数据过滤的基本原则 1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval ...