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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- 转最简便安装python+selenium-webdriver环境方法
最简便安装python+selenium-webdriver环境方法 from:http://www.easonhan.info/python/2013/12/07/active-python-ins ...
- Python复杂多重排序
1. cmp函数是python自带的函数,用于比较两个参数哪个大哪个小 print cmp(2, 3) # -1 如果第一个参数比第二个小,就返回-1,两个元素相等,返回0,否则返回1 2.所以就可以 ...
- 一步步学习ASP.NET MVC3 (1)——基础知识
请注明转载地址:http://www.cnblogs.com/arhat 首先在这里我想声明一下,这个ASP.NET MVC3系列是我在授课过程中的一些经验,有什么不对的地方,请大家指出,我们共同的学 ...
- Get your Windows product key from a script
The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...
- classes system in sencha touch
//http://www.sencha.com/learn/sencha-class-system var Person = new Ext.Class({ name: 'Mr. Unknown', ...
- yum安装软件时提示软件包没有签名
yum install [XXX] -y --nogpgcheck
- JavaScript typeof function()的注意事项
首先,上一段代码: var f = function g() { return 23; }; console.log(typeof g); //输出undefined //console.log(ty ...
- unity 与 android 协调工作 注意事项
原地址:http://blog.csdn.net/u012085988/article/details/17436191 1.unity调android函数 AndroidJavaClass jc = ...
- unity 引用 移动mm 支付sdk
原地址:http://blog.csdn.net/u012085988/article/details/17531005 资源下载:http://download.csdn.net/detail/u0 ...
- 1319-n皇后问题
描述 在n×n 格的棋盘上放置彼此不受攻击的n 个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于在n×n格的棋盘上放置n个皇后,任何2 个皇后不放在同一 ...