Triangle

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.

使用bottom-up方法将最小值汇聚到root,将中间结果保存在开辟的空间curMin中。

class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int m = triangle.size();
if(m == )
return ;
else if(m == )
return triangle[][]; vector<int> curMin = triangle[m-];
for(int i = m-; i >= ; i --)
{// for level i
for(int j = ; j <= i; j ++)
{
curMin[j] = triangle[i][j] + min(curMin[j], curMin[j+]);
}
}
return curMin[];
}
};

【LeetCode】120. Triangle (3 solutions)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. 【LeetCode】120 - Triangle

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

  3. 【一天一道LeetCode】#120. Triangle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【leetcode】610. Triangle Judgement

    原题 A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. ...

  5. 【leetcode】Valid Triangle Number

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  6. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  7. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  8. 【LeetCode】49. Anagrams (2 solutions)

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  9. 【LeetCode】77. Combinations (2 solutions)

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...  ...

随机推荐

  1. Adhoc

    没觉得Adhoc还有什么做的,这几年貌似很冷了,从通信的角度讲,实现比较困难,实际意义不大,国内最近又跟风了VANET.以我同学做的为例,他考虑用Adhoc做野外分散点的自组网(一个集体内),但从通信 ...

  2. eclipse svn 配置

    1.安装svn http://subversion.apache.org/ 到apache网站上下载svn 2.在eclipse中安装svn插件 在eclipse->help->eclip ...

  3. Informatica 常用组件Lookup之二 已连接和未连接的查找

    可以配置一个已连接的查找转换,以从映射管道中直接接收输入:您也可以配置一个未连接的查找转换,以从其它转换的表达式结果中接收输入. 已连接的查找 未连接的查找 直接从管道接收输入值. 从其它转换的 :L ...

  4. Informatica 常用组件Aggregator之二 分组依据端口

    聚合转换允许您为聚合定义组,而不是在所有的输入数据间执行聚合.例如,您可以查找按地区分组的总销量,而不是查找总的公司销量. 要为聚合表达式定义组,请选择聚合转换中的相应输入.输入/输出.输出和变量端口 ...

  5. Android 时间日期Widget 开发详解

    桌面Widget其实就是一个显示一些信息的工具(现在也有人开发了一些有实际操作功能的widget.例如相机widget,可以直接桌面拍照).不过总的来说,widget主要功能就是显示一些信息.我们今天 ...

  6. 检测设备平台,操作系统,方向 Javascript 库:Device.js

    Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <html> 标签添加一些设备平台,操作系统,方向相关的 CSS class,这样就 ...

  7. (转)PlayerPrefs游戏存档

    unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以键值对的形式将数据保存在文件中,然后程序可以根据这个名称取出上次保存的数值.    PlayerPr ...

  8. Install Identity management Database

    Install Identity management Database         Installing Oracle Fusion Applications > Setting up I ...

  9. Linux History安全问题【保存记录防止删除】+完善Linux/UNIX审计 将每个shell命令记入日志

    2011-09-27 22:11:51|  分类: rhel5_033|举报|字号 订阅       Linux利用PROMPT_COMMAND实现审计功能 这个系统审计,记录什么用户,在什么时间,做 ...

  10. POJ_3342_Party_at_Hali-Bula

    #include <iostream> #include <map> #include <cstring> using namespace std; int Gra ...