leetcode—triangle
1.题目描述
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[[2],[3,4],[6,5,7],[4,1,8,3]]The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
2.解法分析
此题最简单的想法就是一个深度搜索,将所有可能的路径全部找出来,比较它们的和,于是有了如下的代码:
class Solution {public:int minimumTotal(vector<vector<int> > &triangle) {// Start typing your C/C++ solution below// DO NOT write int main() function//by areslipan@163.comint minTotal= 0;for(int i = 0;i<triangle.size();++i){minTotal+=triangle[i][0];}triangleHeight = triangle.size();int depth = 1;int curSum = 0;minnum(triangle,minTotal,1,0,curSum);return minTotal;}void minnum(vector<vector<int>> &triangle,int & minTotal,int depth,int loc,int &curSum){curSum+=triangle[depth-1][loc];if(depth == triangleHeight){minTotal = minTotal<curSum?minTotal:curSum;return;}minnum(triangle,minTotal,depth+1,loc,curSum);curSum -= triangle[depth][loc];minnum(triangle,minTotal,depth+1,loc+1,curSum);curSum -= triangle[depth][loc+1];}private:int triangleHeight;};
在小数据集上运行良好,但是大数据集上就不行了,归根结底,深度遍历一方面有重复计算,一方面有递归消耗,再加上是个O(N2)的算法,必定会很慢,不过深度遍历的算法很直观,可以作为进一步分析的基础。既然知道深度搜索不行,那么该怎么办呢,我们发现,其实这个三角是有很强的最优子结构的,分析如下:
如果最短路径通过第i层(最上一层为0层,第一个元素标号为0)的第j个元素,那么必然有最短路径通过第i层的第j-1或者第j个元素,这种二选一的情形是由题意限定的,对于每一层的首尾需特殊处理,如果最短路径通过i层的首尾元素,说明最优路径在上一层的节点已经确定。于是,若已知截止于第i-1层的各个元素的最短路径和SUMi-1(SUM为长度为i的数组),那么截止于第i层的最短路径和SUMi的每个元素可以按照如下公式计算:
- SUMi[j] = min(SUMi-1[j-1],SUMi-1[j]) +triangle[i][j] 若 j>0且j<i
- SUMi[0] = SUMi-1[0]+triangle[0][0];
- SUMi[i] =SUMi-1[i-1] +triangle[i][i];
于是有了以下的代码:
class Solution {public:int minimumTotal(vector<vector<int> > &triangle) {// Start typing your C/C++ solution below// DO NOT write int main() function//areslipan@163.comint triangleHeight = triangle.size();if(triangleHeight==0)return 0;//申请一个长度为n的辅助空间,作为动态规划的备忘表//备忘表被循环利用,第i个循环中只有前i个元素有意义,存放的是遍历到第i层的最佳路径对应的最小值vector<int> mmtTable;mmtTable.assign(triangleHeight,0);mmtTable[0]= triangle[0][0];int tmp = 0;for(int i = 1;i<triangleHeight;++i){//特殊处理每一层的第一个元素,因为第一个元素的上一个节点一定是上一层的第一个元素int cur = mmtTable[0];mmtTable[0] = cur + triangle[i][0];//第i层的中间节点j可能上一层节点是第i-1层的j-1和j个节点for(int j=1;j<i;++j){tmp = mmtTable[j];mmtTable[j] = min(cur,mmtTable[j])+triangle[i][j];cur = tmp;}//特殊处理每一层的最后一个元素,因为最后一个元素的上一个节点一定是上一层的最后一个元素mmtTable[i] = cur+triangle[i][i];}vector<int>::iterator iter;int minTotal = mmtTable[0];for(iter = mmtTable.begin();iter!=mmtTable.end();++iter){if((*iter)<minTotal)minTotal = *iter;}return minTotal;}};
leetcode—triangle的更多相关文章
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode — triangle
/** * Source : https://oj.leetcode.com/problems/triangle/ * * * Given a triangle, find the minimum p ...
- [leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...
- LeetCode - Triangle
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode Triangle及其思考
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode Triangle 三角形(最短路)
题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- mysql更改数据文件目录及my.ini位置| MySQL命令详解
需求:更改mysql数据数据文件目录及my.ini位置. 步骤: 1.查找my.ini位置,可通过windows服务所对应mysql启动项,查看其对应属性->可执行文件路径,获取my.ini路径 ...
- (转载)SQL语句导入导出大全
SQL语句导入导出大全 /******* 导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c ...
- IOS中如何判断APP是否安装后首次运行或升级后首次运行
对于是否为首次安装的App可以使用如下方法来判断 [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] ...
- WPF中Application.Current的使用
WPF程序对应一个Application对象,当前的Application对象可以通过Application.Current获取,通过获取到的Application对象,我们可以做以下的事情: App ...
- poj 2187 Beauty Contest
Beauty Contest 题意:给你一个数据范围在2~5e4范围内的横纵坐标在-1e4~1e4的点,问你任意两点之间的距离的最大值的平方等于多少? 一道卡壳凸包的模板题,也是第一次写计算几何的题, ...
- 在树莓派上部署asp.net
今天成功的在树莓派上部署asp.net呢.之前在unbuntu上测试成功了,结果今天操作的时候又不会操作了,主要对Linux太不熟悉了,找资料,资料又不多,这次赶紧记录下来,以备下次查阅. 我用的mo ...
- VS2015下的Android开发系列01——开发环境配置及注意事项
概述 VS自2015把Xamarin集成进去后搞Android开发就爽了,不过这安装VS2015完成的时候却是长了不知道多少.废话少说进正题,VS2015安装时注意把Android相关的组件勾选安装, ...
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- 网页上PNG透明图片的ie6bug
只有IE6有这个Bug,所以的写法这样就可以了 #png{background:url(../images/png32.png) no-repeat;_filter:progid:DXImageTra ...
- [转载]常用Web Service汇总(天气预报、时刻表等)
下面总结了一些常用的Web Service,是平时乱逛时收集的,希望对大家有用. ============================================ 天气预报Web Servic ...