120. Triangle(Array; DP)
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)的更多相关文章
- 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- ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- 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 ], 这样 ...
- 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 ...
- [leetcode DP]120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题
cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...
- mysql 字符集排查
mysql 字符集排查 库级别 SELECT * FROM information_schema.schemata WHERE schema_name NOT IN ( 'information_sc ...
- javascript的事件流
事件流包括三个阶段: 1.事件捕获阶段 2.处于目标阶段 3.事件冒泡阶段 1.事件捕获阶段 现在页面中有一个按钮. 如果单击这个按钮的话,在事件捕获过程中,document会首先接收到click事件 ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- python应用之爬虫实战1 爬虫基本原理
知识内容: 1.爬虫是什么 2.爬虫的基本流程 3.request和response 4.python爬虫工具 参考:http://www.cnblogs.com/linhaifeng/article ...
- 本地同时安装python2和python3时pip报错
引言: 安装完成后,想测试一下两个版本的pip是否都可以正常工作,结果python3的能正常工作,但是pip2 --version就会报错,报错信息如下: Traceback (most recent ...
- 温故而知新-mysql的一些语法show,describe,explain,fulltext
1 show show tables; 显示数据库的所有表 show databases; 显示所有数据库 show columns from table; 显示表的所有列 show grants f ...
- php读取word里面的内容antiword
其实是现在一个linux下的扩展 1 先安装 antiword yum antiword install 2 写测试php代码 header("Content-type: text/htm ...
- 8. mybatis实战教程(mybatis in action)之七:实现mybatis分页(源码下载)
转自:https://blog.csdn.net/tangruyi1992/article/details/52584012 上 一篇文章里已经讲到了mybatis与spring MVC的集成,并且做 ...
- bootStrap的小知识
代码模块 <code> 内联代码 <kbd> 用户输入 <pre>代码段 <var>数学字符 <samp>程序输出 表格 <thead ...