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. iOS - 果冻效果

    具体使用的CADisplayLink和贝塞尔曲线 下载地址:https://github.com/nLoser/CustomAnimation 效果: // // DisplayView.m // C ...

  2. Linux C/C++ --- “” and <> in the use of head include file(Pending Verification)

    for example: #include <stdlib.h>#include <stdio.h>#include <wiringPi.h>#include &l ...

  3. oracle--知识点汇总1

    同义词: -- e是scott.emp表的临时别名 select e.* from (select * from scott.emp) e; --创建私有同义词 create synonym myem ...

  4. ELKstack搭建

    开源实时日志分析ELK平台部署 官网地址:https://www.elastic.co/products 介绍: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现 ...

  5. 从Paxos到ZooKeeper-一、分布式架构

    本系列为本人读<从Paxos到ZooKeeper>一书的一些读书笔记,仅供学习使用,谢谢. 一.从集中式到分布式 1.1 分布式的定义: 分布式系统是一个硬件或软件组件分布在不同的网络计算 ...

  6. php装饰器模式完成文章编辑

    <?php //文章父类 class BaseArt{ protected $content; protected $art; public function __construct($cont ...

  7. centos'的yum安装php的memcache扩展

    centos'的yum安装php的memcache扩展 博客分类: linux   让php能使用memcached服务的扩展有两种:memcache 和 memcached 1. 先安装libmem ...

  8. DllImport dll中有些啥函数 及 dll中是否用到了别的dll

    在加载dll的时候不知道dll中有哪些接口怎么办,或者使用别人封装的东西时报出类似于“无法在 DLL“XXX.dll”中找到名为“XXX函数”的入口点.”     1.通过LordPE这个软件来看dl ...

  9. Runner站立会议08

    会议时间:2016.4.27  21.10~21.25 地点:基教负一层 今天:看日历的代码,网上下的,没有注释 明天:继续看代码 困难:代码看不懂 会议照片: 燃尽图:

  10. 有了这个,再也不用每次连新机器都要设置secure crt属性了

    我连服务器用的是secure crt,每次ssh新服务器的时候都得手动设置字符编码和背景颜色,今天问了旁边的开发原来可以全局设置,以后连服务器的时候就再也不用手动设置相关属性了.步骤如下: 一开始点击 ...