题意:

  给一个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. jQuery学习小结1-CSS操作+事件

    一.DOM对象和jQuery 对象互换 1.jQuery对象 就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的,其可以使用jQuery里的方法.比如: $(&quo ...

  2. BZOJ3307 雨天的尾巴

    首先考虑序列怎么做... 只要把操作差分了,记录在每个点上 然后维护一棵权值线段树,表示每个颜色出现的次数,支持单点修改和查询最大值操作 只要把序列扫一遍就好了,时间复杂度$O(n + m*logZ) ...

  3. double int char 数据类型

    贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...

  4. spring mvc设置字符集过滤器

    <filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...

  5. windows下mysql主从同步备份步骤

    目的:有两台MySQL数据库服务器A和B,使A为主服务器,B为从服务器,初始状态时,A和B中的数据信息相同,当A中的数据发生变化时,B也跟着发生相应的变化,使得A和B的数据信息同步,达到备份的目的. ...

  6. java 面向对象编程 第18章——网络编程

    1.  TCP/IP协议模型 应用层:应用程序: 传输层:将数据套接端口,提供端到端的通信服务: 网络互联层:负责数据包装.寻址和路由,同时还包含网间控制报文协议: 网络接口层:提供TCP/IP协议的 ...

  7. C#多线程学习之(五)使用定时器进行多线程的自动管理

    本文实例讲述了C#多线程学习之使用定时器进行多线程的自动管理.分享给大家供大家参考.具体分析如下: Timer类:设置一个定时器,定时执行用户指定的函数. 定时器启动后,系统将自动建立一个新的线程,执 ...

  8. STL 自学

    STL 一.vector动态数组 1 包含头函数 #include<vector> 2 函数的声明: vector<int> v; vector<int> v[ma ...

  9. iOS中Block使用探索

    Block介绍 Block在ios 4.0之后加入,并大量使用在新的ios api中.block是一个匿名的代码块,可以传递给其他对象的参数,并得到返回值.从本质上讲,block同其他普通的变量类似, ...

  10. python 第三方库 chardet

    chardet是一个非常优秀的编码识别模块.chardet 是python的第三方库,需要下载和安装,放在python安装根目录\Lib\site-packages下面 import chardet ...