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. seaborn可视化特征的相关性

    import seaborn as sn sn.heatmap(trainX.corr(),vmax=1,square=True)

  2. BCGcontrolBar(五) 对话框大小改变控件自动适应

    改变控件大小 首先在 构造函数中加入 EnableLayout(); 在OnInitDialog()函数中加入 CBCGPStaticLayout* pLayout = (CBCGPStaticLay ...

  3. HTC Vive的定位技术

    Lighthouse空间定位,chaperone系统避免实际障碍物 HTC vive所用的Lighthouse技术属于激光定位技术,Oculus Rift以及索尼PlayStation VR所用的定位 ...

  4. 详解 Facebook 田渊栋 NIPS2017 论文:深度强化学习研究的 ELF 平台

    这周,机器学习顶级会议 NIPS 2017 的论文评审结果已经通知到各位论文作者了,许多作者都马上发 Facebook/Twitter/Blog/ 朋友圈分享了论文被收录的喜讯.大家的熟人 Faceb ...

  5. linux操作系统5 shell编程

    知识内容: 1.shell编程预备知识 2.shell变量 3.表达式与运算符 4.分支循环语句 5.函数 一.shell编程预备知识 1.什么是shell编程 shell是与linux交互的基本工具 ...

  6. Convolutional Neural Networks

    卷积神经网络(Convolutional Neural Networks/ CNN/ConvNets) 卷积神经网络和普通神经网络十分相似: 组成它们的神经元都具有可学习的权重(weights)和偏置 ...

  7. tornado-模板继承extend,函数和类的导入

    大 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # ...

  8. DBCP连接池配置常用参数说明

    参数 默认值 说明 username \ 传递给JDBC驱动的用于建立连接的用户名 password \ 传递给JDBC驱动的用于建立连接的密码 url \ 传递给JDBC驱动的用于建立连接的URL ...

  9. 过滤器 ;spring拦截器 切片 小结

    1. springMVc的拦截器 实现HandlerInterceptor接口,如下: public class HandlerInterceptor1 implements HandlerInter ...

  10. leetcode344

    public class Solution { public string ReverseString(string s) { var list = s.Reverse(); StringBuilde ...