LeetCode Minimum Path Sum (简单DP)
题意:
给一个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)的更多相关文章
- 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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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 最小路径和
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 @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 开源软件架构总结之——Bash(readline做输入交互式,词法语法分析,进程交互)
第3章 The Bourne-Again Shell Bash的主要组件:输入处理,解析,单词展开(word expansion)和其他命令处理,管道(pipeline)中的命令执行.这些组件构成一个 ...
- treap 1296 营业额统计
有一个点答案错误,求大神指教 #include<cstdio>#include<iostream>#include<cstdlib>#include<ctim ...
- C- printf的使用
ASC C之后引入的一个特性是,相邻的字符可以被自动连接 /* printf.cc * 2014/09/02 update */ #include <iostream> using nam ...
- The Coco-Cola Store C(Contest #3 )
Once upon a time, there is a special coco-cola store. If you return three empty bottles to the shop, ...
- Linux下screen命令
//1.列出当前的screenscreen -ls //2.新建一个screen,直接在命令行键入screen命令 screen -S [会话名称][root@www.lnuxidc.com ~]# ...
- Xp 消息队列的使用
1.安装消息队列3.0: 控制面板/添加删除程序/添加window组件/找到消息队列/选择->详细信息->MSMQ HTTP支持. 注意:如果计算机没有连接到域需要去掉Active Dir ...
- poj2955 区间dp
//Accepted 200 KB 63 ms //区间dp //dp[i][j] 从i位到j位能得到的最大匹配数 //dp[i][j]=max(dp[i+1][j-1] (s[i-1]==s[j-1 ...
- FMDB数据库中的一些操作
#pragma mark - 数据库的操作 - (BOOL)judgeModel:(TaskResourceModel *)model isInArray:(NSArray *)shopArray { ...
- 关于oracle存储过程的一些知识点
一.创建一个存储过程,批量清空数据库中所有表的数据. --清空数据库中所有表的数据 create or replace procedure truncateAllTables as v_sql ); ...
- Xtreme Toolkit Pro 免费下载地址
Xtreme Toolkit Pro 是针对Windows程序员的一套先进的用户界面套包,强大的功能可使您的应用程序具有专业的.现代感的外观. Xtreme Toolkit Pro 由8个专业级的构件 ...