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的更多相关文章

  1. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. Triangle——LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. Pascal's Triangle leetcode

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. Triangle LeetCode |My solution

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. Pascal's Triangle leetcode java(杨辉三角)

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  6. Triangle leetcode java

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  7. triangle leetcode C++

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. LeetCode第二天&第三天

    leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...

随机推荐

  1. 【Beta】Scrum01

    Info 时间:2016.11.26 21:30 时长:10min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.11.28 21:30 Task Report Name ...

  2. zabbix使用介绍

    zabbix自定义监控项 1.创建主机组,可以根据redis.mysql.web等创建对于的主机组 2.创建主机 3.创建Screens 4.自定义监控项 zabbix_agentd.conf配置文件 ...

  3. 创建一个应用台程序,声明一个 int变量,并且初始化数组,通过while语句输出数组内的所有成员。

    ]{,,,,}; ; ) { Console.WriteLine("myNum[{0}]的值为{1}", s,myNum[s]); s++; } Console.ReadLine( ...

  4. Github for Windows使用介绍

    Git已经变得非常流行,连Codeplex现在也已经主推Git.Github上更是充斥着各种高质量的开源项目,比如ruby on rails,cocos2d等等.对于习惯Windows图形界面的程序员 ...

  5. JavaScript学习笔记——对象知识点

    javascript对象的遍历.内存分布和封装特性 一.javascript对象遍历 1.javascript属性访问 对象.属性 对象[属性] //字符串格式 //javascript属性的访问方法 ...

  6. UVa 11988 Broken Keyboard(链表->数组实现)

    /*数组形式描述链表:链表不一定要用指针. 题目链接:UVa 11988 Broken Keyboard 题目大意: 小明没有开屏幕输入一个字符串,电脑键盘出现了问题会不定时的录入 home end ...

  7. 一句SQL实现获取自增列操作

    @@IDENTITY返回最后插入的标识值. 语法@@IDENTITY 返回类型numeric 注释在一条 INSERT.SELECT INTO 或大容量复制语句完成后,@@IDENTITY 中包含此语 ...

  8. chain.doFilter(request,response)含义

    过滤器的生命周期一般都要经过下面三个阶段: 初始化 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上并 ...

  9. MongoDB 3.0.6 安装 增删改查

    下载 安装包MSI http://yunpan.cn/cmhHdTPkXZRM2  访问密码 9b6c 上边提供的是 MongoDB 3.0.6 64Bit 的安装包 安装 如果不想直接安装在C盘.. ...

  10. 做技术最自由,在IT最幸福!

    这些天来,一直感觉"做技术最自由,在IT最幸福!" 在IT最幸福 一直感觉从事IT行业最幸福,想想那些干机械的.干汽修.地勤的,让我干那些工作,对我来说真是折磨! 大体总结以下几点 ...