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.

这个问题分为两种情况,一种不可以修改原数组元素,另一种可以修改数组元素

1、可以修改数组元素

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

2、不能修改数组元素

LeetCode -- Triangle 路径求最小和( 动态规划问题)的更多相关文章

  1. [LeetCode] Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. [LeetCode] Triangle 三角形

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

  3. hiho 第116周,最大流最小割定理,求最小割集S,T

    小Hi:在上一周的Hiho一下中我们初步讲解了网络流的概念以及常规解法,小Ho你还记得内容么? 小Ho:我记得!网络流就是给定了一张图G=(V,E),以及源点s和汇点t.每一条边e(u,v)具有容量c ...

  4. [LeetCode] Smallest Good Base 最小的好基数

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  5. [leetcode]Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...

  6. POJ 2253 Frogger【最短路变形——路径上最小的最大权】

    链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  8. poj1797(dijstra变形,求最小边的最大值)

    题目链接:https://vjudge.net/problem/POJ-1797 题意:n个点,m条带权边,求点1到点n的所有路径中最小边的最大值. 思路: 和poj2253一样,只不过那题n< ...

  9. Leetcode 不同路径系列

    Leetcode不同路径系列题解笔记 62. 不同路径 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 "Start" ). 机器人每次只能向下或者向右移动一 ...

随机推荐

  1. 【BZOJ】1059: [ZJOI2007]矩阵游戏(二分图匹配)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1059 本题可以看出,无论怎样变化,在同一行和同一列的数永远都不会分手---还是吐槽,,我第一眼yy了 ...

  2. linux 好用的程序

    来自   http://www.cnblogs.com/skyseraph/archive/2010/10/30/1865280.html * Shell: bash.我使用 bash 的 vi 命令 ...

  3. Shell 操作练习2

    #! /bin/sh ############################### # -- # # author jackluo # # net.webjoy@gmail.com # ###### ...

  4. WritePrivateProfileString()

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 将信息写入.INI文件中 1.所用的WINAPI函 ...

  5. 【iHMI43 4.3寸液晶模块】demo例程(版本1.02)发布

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  6. 网易前端JavaScript编码规范【转】

    1. 变量命名规范 变量名包括全局变量,局部变量,类变量,函数参数等等,他们都属于这一类. 基本规范 变量命名都以类型前缀+有意义的单词组成,单词首字母都需要大写.例如:sUserName,nCoun ...

  7. JavaScript系列:再巩固-原型链

    图 1.实例:'对象(实例)'有属性__proto__,指向该对象(实例)的'构造函数的原型对象'. 2.方法:'构造函数'除了有属性__proto__,所有构造函数方法的__proto__指向Fun ...

  8. linphone3.4.0代码分析

    主要类型定义: 1.LinphoneCoreVTable /** * This structure holds all callbacks that the application should im ...

  9. c/c++内存调试

    Leaktracer,Valgrind,ElectricFence 内存泄漏分类 以发生的方式来分类,内存泄漏可以分为4类: 常发性 发生内存泄漏的代码会被多次执行到,每次被执行的时候都会导致一块内存 ...

  10. 常用Ubuntu 命令

    sudo apt-get updatesudo apt-get upgrade以下2選1sudo apt-get install ubuntu-desktop如果你只想安装必要的桌面管理软件而不想安装 ...