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. [转][Java] Date 格式化

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...

  2. [UE4]计算2点坐标附近的坐标:线性插值法

    float distance = FVector::Distance(SelfLocation, TargetLocation); .f / distance; DrawDebugPoint(GetW ...

  3. API网关Kong系列(一)初识

    最近工作需要,加上国内Kong的文章相对缺乏(搜来搜去就那么两篇文章),而且官方文档在某些demo上也有一些过时的地方,遂提笔记录下这些,希望能有帮助. 先随大流介绍下KONG(主要参考官网): 官方 ...

  4. 在ie6下的png图片的兼容问题

    png图片在ie6下是这样的: 正确样式: 这样解决: html代码: <body> <div class="gys"></div> </ ...

  5. CSS个人笔记

    1. CSS盒模型 1.1 控制元素尺寸属性 1.1.1 box-sizing: 改变元素应用的尺寸规则 当设置元素尺寸宽度为固定值时(eg: 100px), 其实是元素内容区域的宽度为100px, ...

  6. EF中GroupBy扩展方法的简单使用

    public ActionResult ShopInfo() { ViewBag.ShopList = ShopService.GetEntities(x => x.IsDelete == fa ...

  7. ubuntu16.04安装python3,numpy,pandas等量化计算库

    ubunt安装python3 sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install ...

  8. 0_Simple__UnifiedMemoryStreams

    使用 OpenMP 和 pthreads 两种环境,利用实现统一内存编址,计算基本的矩阵乘法 result = α * A * x + β * result . ▶ 源代码 #include < ...

  9. SparkSession

    在2.0版本之前,使用Spark必须先创建SparkConf和SparkContext catalog:目录 Spark2.0中引入了SparkSession的概念,SparkConf.SparkCo ...

  10. leetcode942

    public class Solution { public int[] DiStringMatch(string S) { var len = S.Length; ; var max = len; ...