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.

思路:与119题类似,只是现在DP中存储的不是数组的值,而是到目前为止的minimum sum。赋值DP时为了避免改变上一行结果,也是得从右往左

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

120. Triangle(Array; DP)的更多相关文章

  1. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

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

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

  3. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  4. Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题

    F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...

  5. Java for LeetCode 120 Triangle

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

  6. [leetcode DP]120. Triangle

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

  7. LeetCode 120. Triangle (三角形)

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

  8. LeetCode - 120. Triangle

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

  9. leetcode 120 Triangle ----- java

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

随机推荐

  1. 【整理总结】代码沉淀 - CefSharp - 比较流行的第三方内嵌浏览器组件

    .NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework web: https://github.com/ce ...

  2. docker 基础操作

    1. 安装docker 系统centos 7.2 yum -y install docker-io service docker start 安装完毕后执行 docker version 或者dock ...

  3. 1053 Path of Equal Weight (30 分)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  4. web前端开发企业级CSS常用命名,书写规范总结

    1.常用命名 标题: title 摘要: summary 箭头: arrow 商标: label 网站标志: logo 转角/圆角: corner 横幅广告: banner 子菜单: subMenu ...

  5. 【Codeforces】CF 2 B The least round way(dp)

    题目 传送门:QWQ 分析 求结尾0的数量QwQ. 10只能是$ 2 \times 5 $,我们预处理出每个数因子中2和5的数量. 我们接着dp出从左上到右下的经过的最少的2的数量和最少的5的数量.两 ...

  6. git本地项目上传远程

    Git的安装就不说了. 原文:https://blog.csdn.net/zamamiro/article/details/70172900 github官网说明: …or create a new ...

  7. python中os常用方法

    python中OS常用方法 Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问 ...

  8. Python3 open()函数参数

    open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=No ...

  9. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

  10. 关于THINKPHP5模型关联的初步理解

    初步理解的意思是,使用最常用的关联模型,然后可以正常运行 还是打个比方 文章表  和文章分类表 一个文章分类可以有多个文章  所以  文章分类模型和文章建立 hasMany的关联 而文章和文章分类表则 ...