leetcode第一刷_Minimum Path Sum
能够用递归简洁的写出,可是会超时。
dp嘛。这个问题须要从后往前算,最右下角的小规模是已知的,边界也非常明显,是最后一行和最后一列,行走方向的限制决定了这些位置的走法是唯一的,能够先算出来。然后不断的往前推算。
用distance[i][j]保存从当前位置走到最右下角所需的最短距离,状态转移方程是从distance[i+1][j]和distance[i][j+1]中选一个小的,然后再加上自身的。
代码非常easy理解,这就是dp的魅力。空间上是能够优化的,由于当前状态仅仅与后一行和后一列有关系。
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int erow = grid.size();
if(erow<=0) return 0;
int ecolumn = grid[0].size();
if(ecolumn<=0) return 0;
if(erow==1&&ecolumn==1) return grid[0][0];
vector<int> tpres(ecolumn, 0);
vector<vector<int> > distance(erow, tpres);
distance[erow-1][ecolumn-1] = grid[erow-1][ecolumn-1];
for(int i=erow-2;i>=0;i--)
distance[i][ecolumn-1] = distance[i+1][ecolumn-1] + grid[i][ecolumn-1];
for(int i=ecolumn-2;i>=0;i--)
distance[erow-1][i] = distance[erow-1][i+1] + grid[erow-1][i];
for(int i=erow-2;i>=0;i--){
for(int j=ecolumn-2;j>=0;j--){
distance[i][j] = min(distance[i+1][j], distance[i][j+1])+grid[i][j];
}
}
return distance[0][0];
}
};
leetcode第一刷_Minimum Path Sum的更多相关文章
- leetcode第一刷_Simplify Path
这道题的思路还是比較清晰的,用栈嘛,麻烦是麻烦在这些层次的细节上.主要有以下几个: ./和/:当前路径,遇到这样的,应该将后面的文件夹或文件入栈. ../:上一层路径.遇到这样的.应该做一次出栈操作, ...
- leetcode第一刷_Minimum Window Substring
好题.字符串.线性时间. 我认为第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符全然包括了另一个给定目标串中的字 ...
- leetcode第一刷_Minimum Depth of Binary Tree
非常easy的题目.只是还是认为要说一下. 最小深度.非常快想到bfs,层序遍历嘛.本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息.后来想到能够压入一个特殊的对象,每次到达这个对象 ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- leetcode–Binary Tree Maximum Path Sum
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
- 【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 ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
随机推荐
- 32位Linux文件限制大小
线上程序不断重新启动,查看log发现是进程由于SIGXFSZ信号退出.对过大的文件进行操作的时候会产生此信号,一般仅仅在32位机器上出现,文件限制大小为2G.用lsof查看进程打开的文件,果然有一个文 ...
- 开源一个适用iOS的数据库表结构更新机制的代码
将前段时间开源的代码.公布一下: ARDBConfig On the iOS, provide a database table structure update mechanism, ensure ...
- Android多线程文件下载器
本应用实现的是输入文件的网络的地址,点击button開始下载,下载过程中有进度条和后面的文本提示进度, 下载过程中button不可点击,防止反复的下载,完成下载后会进行Toast的提示显示, 而且回复 ...
- 使用 SQLNET.EXPIRE_TIME 清除僵死连接
数据库连接的客户端异常断开后,其占有的相应并没有被释放,如从v$session视图中依旧可以看到对应的session处于inactive,且对应的服务器进程也没有释放,导致资源长时间地被占用,对于这种 ...
- .net数据根据字段进行分类(linq语句)
var items = List<实体>; var models = items.GroupBy(r => r.分类字段).ToDictionary(d => d.Key, d ...
- AMDU恢复ASM磁盘组数据(測)
--umount ASMCMD> umoung -a asmdg commands: md_backup, md_restor lsattr, setattr ...
- BZOJ 1038 ZJOI2008 瞭望塔 半平面交
题目大意及模拟退火题解:见 http://blog.csdn.net/popoqqq/article/details/39340759 这次用半平面交写了一遍--求出半平面交之后.枚举原图和半平面交的 ...
- 快捷找jar包方式
通过例如以下站点查找jar包: 1.http://www.java2s.com/Code/Jar/o/Downloadorgapacheservicemixbundlescommonslang246j ...
- VMware GSX Server 3.2.1 Build 19281免费下载
VMware GSX Server 3.2.1 Build 19281免费下载 评论2 字号:大中小 订阅 VMware官方下载: For Windows 版系统:http://download3 ...
- 飘逸的python - 发送带各种类型附件的邮件
上一篇博文演示了如何发送简单的邮件,这一篇将演示如何发送各种类型的附件. 基本思路就是,使用MIMEMultipart来标示这个邮件是多个部分组成的,然后attach各个部分.如果是附件,则add_h ...