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. Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信

    1.Jni不在赘述.翻看前面博客 2.直接上代码 1)Java层,直接加在AppActivity.java中 public class AppActivity extends Cocos2dxActi ...

  2. OpenStreetMap地图数据介绍(转)

    原文链接:每日一读 Packtpub.OpenStreetMap(1) 相信绝大多数人都知道Wiki,但要提起地图,大家第一反应肯定是Google地图.在没看这本书之前,还真不知原来还有OpenStr ...

  3. mongodb实现远程连接

    mongodb远程连接配置分为以下4步: 1. 添加管理员账户 > use admin switched to db admin > db.addUser('tank','test'); ...

  4. 混沌数学之Chua's circuit(蔡氏电路)

    蔡氏电路(英语:Chua's circuit),一种简单的非线性电子电路设计,它可以表现出标准的混沌理论行为.在1983年,由蔡少棠教授发表,当时他正在日本早稻田大学担任访问学者[1].这个电路的制作 ...

  5. pytorch 学习问题

    https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#sphx-glr-beginner-blitz-n ...

  6. JAVASCRIPT校验大全[转]

    var IsFireFox = document.getElementById &&! document.all;//判断是否为FireFox //页面里回车到下一控件的焦点 func ...

  7. 记录C#错误日志工具

    在编程过程中,我们经常会用try...catch处理可能出错的代码块.如果程序出现错误,则直接show出错误信息. 当然,大型的系统都有错误日志处理模块,用数据库记录错误日志信息,有相应的写入错误日志 ...

  8. 修改一行SQL代码 性能提升了100倍

    在PostgreSQL中修改了一行不明显的代码,把(ANY(ARRAY[...]) 改成 ANY(VALUES(...))),结果查询时间从20s变为0.2s.最初我们学习使用 EXPLAN ANAL ...

  9. 为什么空格拷贝到linux 会变成两个

    为什么空格拷贝到linux 会变成两个 学习了:https://zhidao.baidu.com/question/266438357.html 在vi界面内输入:set paste 然后进行拷贝: ...

  10. php5.2以下版本无json_decode函数的解决办法

    function json_decode2($json) { $comment = false; $out = '$x=';   for ($i=0; $i<strlen($json); $i+ ...