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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- ECshop网点程序优化-自动生成类目页Keywords、Desciption Meta
ECshop支持针对每个新建的类目自定义Keywords.Description Meta信息,好处就不用说了,帮助SEO或者让浏览者了解这是什么页面,但如果有几百个类目的时候,人工去写这些类目又有点 ...
- 一些web编程能用到的小知识
1 信用卡验证算法-luhn算法.(in django/utils/checksums.py) 1.从卡号最后一位数字开始,逆向将奇数位(1.3.5等等)相加.2.将偶数位数字相加,但是这里有个麻烦. ...
- Python的传值和传址与copy和deepcopy
1.传值和传址 传值就是传入一个参数的值,传址就是传入一个参数的地址,也就是内存的地址(相当于指针).他们的区别是如果函数里面对传入的参数重新赋值,函数外的全局变量是否相应改变,用传值传入的参数是不会 ...
- 几条特殊的SQL语句
1, 有case情况. select trunc(exf_payment_receipt.work_date),exf_payment_receipt.exchange_code,exf_paymen ...
- Command-line tools can be 235x faster than your Hadoop cluster
原文链接:http://aadrake.com/command-line-tools-can-be-235x-faster-than-your-hadoop-cluster.html Introduc ...
- SDUT 1269 走迷宫(BFS)
点我看题目 题意:中文不详述. 思路 :上上上场比赛让一个BFS给虐了,上次比赛让一个三维的给废掉了.......所以急于从水题刷起......还因为数组开小了WA了5,6次 #include < ...
- android 发送自定义广播以及接收自定义广播
发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- 设置Tomcat的UTF-8编码
利用request.setCharacterEncoding("UTF-8");来设置Tomcat接收请求的编码格式,只对POST方式提交的数据有效,对GET方式提交的数据无效! ...
- SQLite入门与分析(五)---Page Cache之并发控制
写在前面:本节主要谈谈SQLite的锁机制,SQLite是基于锁来实现并发控制的,所以本节的内容实际上是属于事务处理的,但是SQLite的锁机制实现非常的简单而巧妙,所以在这里单独讨论一下.如果真正理 ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...