题意:

  给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少?

思路:

  比较水,只是各种空间利用率而已。

  如果可以在原空间上作修改。

 class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int n=grid.size()-;
int m=grid[n].size()-;
for(int j=; j<=m; j++)
grid[][j]+=grid[][j-];
for(int i=; i<=n; i++)
{
grid[i][]+=grid[i-][];
for(int j=; j<=m; j++)
grid[i][j]+=min(grid[i-][j], grid[i][j-]);
}
return grid[n][m];
}
};

AC代码

  

  至少也要用O(m)的空间吧。

 class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
vector<int> dp(grid[].begin(),grid[].end());
int n=grid.size()-, m=grid[n].size()-;
for(int j=; j<=m; j++)
dp[j]+=dp[j-];
for(int i=; i<=n; i++)
{
dp[]+=grid[i][];
for(int j=; j<=m; j++)
dp[j]=grid[i][j]+min(dp[j-], dp[j]);
}
return dp[m];
}
};

AC代码

LeetCode Minimum Path Sum (简单DP)的更多相关文章

  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 ...

  2. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  3. [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 ...

  4. [leetcode]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  5. 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 ...

  6. LeetCode:Minimum Path Sum(网格最大路径和)

    题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  7. 64. Minimum Path Sum (Graph; DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. Minimum Path Sum(DFS,DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. [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 ...

随机推荐

  1. js首字母大写--单个单词的处理方式

    var operate2='OR'; for (var j = 0, len = operate1.length; j< len; j++) { //获得unicode码 var ch2 = o ...

  2. eval()函数使用

    条件:有数据集data[indx],数据集内含有对象data[index].obj1.pama1. 说明:传入参数为var str = 'obj1.pama1',要求取得data[index].obj ...

  3. dll和ocx比较

    ActiveX,OLE是基于COM的一种应用,其文件后缀一般以dll和ocx结尾:ocx作为一种特殊的dll文件,具有一定的用户界面和事件响应,而dll文件只是方法和属性的集合. 一.关于DLL的介绍 ...

  4. [整]常用的几种VS编程插件

    通过这些编程插件,你可以方便快捷的完成编程的各项任务,以下分别作下简单介绍,欢迎讨论交流. Visual Assist(强烈推荐)网址:http://www.wholetomato.com/功能:VA ...

  5. outlook 用宏发邮件

    经常发面试邮件,通常只是修改一下收件人邮箱地址,和收件人姓名,其他全部一致,有木有发现每次都用用outlook写邮件很麻烦? 使用宏发邮件,就会不麻烦了,直接修改下称呼,修改下收件人地址,按下F5,就 ...

  6. C++string的操作

    #include <iostream> using namespace std; int main() { //initilization string str("abc.ddd ...

  7. [vijos P1626] 爱在心中

    做完Victoria的舞会3,挑了vijos里强连通分量里面难度值最低的题目,也就是这道.先把第一小问做了,纯Tarjan,只是我学的时候的标程是用邻接表的,这题数据小于是用了邻接矩阵,两者之间的切换 ...

  8. poj2392 多重背包

    //Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...

  9. zoj1530 bfs

    //Accepted zoj1530 270ms 40008KB #include <cstdio> #include <cstring> #include <iostr ...

  10. hdu2476 区间dp

    //Accepted 300 KB 31 ms //区间dp 思路完全网上看的 #include <cstdio> #include <cstring> #include &l ...