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 ...
随机推荐
- HDU 2159 FATE(全然背包+二维费用背包)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Java基础知识强化58:经典排序之二叉树排序(BinaryTreeSort)
1. 二叉树排序 二叉树排序的描述也是一个递归的描述, 所以二叉树排序的构造自然也用递归的: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它 ...
- SpringMVC学习简单HelloWorld实例
首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubuntu12.04(不同操作系统对本系列项目没有影响): 开发工具:Eclipse For JavaEE: 数据库:My ...
- Linux 系统库函数coreleft 与sbrk简介
coreleft 函数名: coreleft 功 能: 返回未使用内存的大小 用 法: unsigned coreleft(void); 程序例: #include <stdio.h> ...
- Edwin windows下基本命令:
Ctrl-Alt-z: 对区域内所有代码求值. Ctrl-x Ctrl-e: 对光标左边或上一个表达式求值. Ctrl-c Ctrl-x: 中断当前求值. Ctrl-a: 移动到行首. Ctrl-e: ...
- StringEscapeUtils的使用
使用commons-lang.jar import org.apache.commons.lang.StringEscapeUtils; public class T { public static ...
- Oracle ACL (Access Control List)详解
在Oracle11g中,Oracle在安全方面有了很多的改进,而在网络权限控制方面,也有一个新的概念提出来,叫做ACL(Access Control List), 这是一种细粒度的权限控制.在ACL之 ...
- (原)torch使用caffe时,提示CUDNN_STATUS_EXECUTION_FAILED
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6230227.html 提前说明:此文不能真正解决该问题,具体原因我也不知道... 以前使用某台电脑A上 ...
- C# 解析json Newtonsoft.Json
Newtonsoft.Json.dll public class ErrorInfo { public error_response error_response { get; set; } } pu ...
- 在YouCompleteMe+Syntastic中添加和取消对C++11的支持
添加对c++11的支持: /.vimrc中添加: let g:syntastic_cpp_compiler = 'g++' "change the compiler to g++ to s ...