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.

实现:

class Solution {

public:

    int minimumTotal(vector<vector<int>>& triangle) {

        int n = triangle.size();

        for (int i=n-2; i >=0; i--) {

            for (int j = 0; j < triangle[i].size(); j++) {

                triangle[i][j] += triangle[i+1][j] < triangle[i+1][j+1] ? triangle[i+1][j] : triangle[i+1][j+1];

            } 

        }

        return triangle[0][0];

    }

};

LeetCode120——Triangle的更多相关文章

  1. Leetcode120.Triangle三角形最小路径和

    给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和为 11 ...

  2. LeetCode120 Triangle

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

  3. [Swift]LeetCode120. 三角形最小路径和 | Triangle

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

  4. [LeetCode] Triangle 三角形

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

  5. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. [LeetCode] Pascal's Triangle 杨辉三角

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

  7. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  8. 【leetcode】Pascal's Triangle

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

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. bzoj 4184 shallot 时间线建线段树+vector+线性基

    题目大意 n个时间点 每个时间点可以插入一个权值或删除一个权值 求每个时间点结束后异或最大值 分析 异或最大值用线性基 但是线性基并不支持删除操作 我们可以对时间线建一棵线段树 离线搞出每个权值出现的 ...

  3. window.getComputedStyle——ref

    componentDidMount() { const LeftHeight = window.getComputedStyle(this.leftDom).height; console.log(L ...

  4. [笔记][FPGA]如何使用SignalTap观察wire与reg值

    0. 简介 在FPGA程序调试时,我们除了仿真还经常的会用到SignalTap进行板级调试,其可以真实有效的反应某些变量的变化,方便我们理解内在跳转,方便Debug的运行.SignalTap需要制定时 ...

  5. CMDB与自动化运维,一切尽在掌握中?

    生产力跟不上生产的速度时,就会出现很多问题,如何针对问题进行处理,制定什么样的计划,如何解决就是需要思考的难点? T运维的分类 IT运维,指的是对已经搭建好的网络,软件,硬件进行维护.运维领域也是细分 ...

  6. 蚂蚁金服CTO程立:金融级分布式交易的技术路径

    总结: 强一致的微服务 oceanbase里面的投票选举以及多中心多地部署 单元化市异地多活的基础.支付宝是异地多活和容灾结合,而容灾的基础也是单元化.基于单元化进行单元的调度.部署.容灾. 混合云架 ...

  7. 华硕win7安装ubuntu14.04.02注意事项

    一.win7下划出给ubuntu系统的分区 1.win7自带分磁盘的工具,只需要压缩步骤即可,不需要继续分盘符格式化等操作 win7下为绿色 安装时为free space 二.制作启动盘并安装注意事项 ...

  8. jenkins的Pipeline代码流水线管理

    1.新建一个pipline任务 2.自写一个简单的pipline脚本 a.Pipeline的脚本语法在Pipeline Syntax中,片段生成器,示例步骤中选择builf:Build a job b ...

  9. ArrayList和LinkedList学习

    摘要 ArrayList和LinkedList是对List接口的不同数据结构的实现.它们都是线程不安全的,线程不安全往往出现在数组的扩容.数据添加的时候. 一.ArrayList和LinkedList ...

  10. jquery操作checkbox方法(全选、全不选、至少选择一个、选择值/文本)

    原文:http://blog.csdn.net/u014079773/article/details/52371382 在实际开发中我们经常操作checkbox,不仅仅要获得checkbox选中的值, ...