Triangle leetcode
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).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
解题思路:使用动态规划法。当我们计算第i层的数到底层的最小和时,如果我们知道第i+1层的数到底层最小的和就好算了。即minsum[i][j]=triangle[i]+min( minsum[i+1][j] , minsum[i+1][j+1] );从底层向顶层逐层计算,就能得到最终结果。
本文使用大小为n的数组d记录每一层的结果,达到了O(n)的空间复杂度要求。
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int s = triangle.size();
if(s != (triangle[s-1].size()))
return -1;
if(s==1)
return triangle[0][0];
int *d = new int[s];
int i,j;
for(i=0;i<s;i++)
d[i]=triangle[s-1][i];
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
d[j]=triangle[i][j]+min(d[j],d[j+1]);
}
}
return d[0];
}
};
Triangle leetcode的更多相关文章
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle——LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Triangle LeetCode |My solution
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Triangle leetcode java
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- triangle leetcode C++
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
随机推荐
- SQLite的WAL机制
标注:本文部分有黏贴这里的资料,另外还加了一些自己的笔记 使用CoreData或者SQLite3的时候,我们创建的数据库, 在存储的文件夹中有三个文件:分别为:**.sqlite **.sqlite ...
- BZOJ3506/1502 [CQOI2014]排序机械臂
传送门 依然是一道splay的区间操作,需要注意的是要把下标离散化后来表示splay的节点,我不知道怎么搞所以索性弄了个$ValuetoNode$,看样子没什么问题, 感觉他那个传下标的方法太暴力了. ...
- 【原创】一段简短的读取libglade的UI文件的Python代码
准备写一个将Glade/GtkBuilder等格式的UI文件转换成C++代码的python程序 首先完成的是将LIBGlade格式读取至内存中 #!/usr/bin/env python # -*- ...
- 蛋疼的vs
这个vs2008 难用的很,要是叫我选肯定vs高版本的,vs2012或者直接vs2015
- 解决git pull 命令失效,不能从远程服务器上拉取代码问题
用时候在用Git pull命令的时候不管用,拉取不下来远程分支上的代码,是因为本地分支和远程分支没有建立关联. 处理这种问题很简单就按照提示执行命令即可:git branch --set-upstre ...
- python 传递结构体指针到 c++ dll
CMakeLists.txt # project(工程名) project(xxx) # add_library(链接库名称 SHARED 链接库代码) add_library(xxx SHARED ...
- jquery特效收藏
js网址收藏: 懒人图库:www.lanrentuku.com 懒人之家:http://www.lanrenzhijia.com/jquery/list_5_2.html 1.UI下载:http:// ...
- php字符串常用算法--字符串加密解密
/** * 加密.解密字符串 * * @global string $db_hash * @global array $pwServer * @param $string 待处理字符串 * @para ...
- Database Initialization Strategies in Code-First:
You already created a database after running your Code-First application the first time, but what ab ...
- centos 7.0 查看根目录下所有文件夹
centos 7.0最小化安装 第一行是登录 [root@localhost ~]# [root@localhost ~]# cd ../ [root@localhost /]# ls bin dev ...