triangle leetcode C++
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 is11(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.
C++
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int n = triangle.size();
vector<int> dp(triangle.back());
for (int i = n - 2; i >= 0; --i)
for (int j = 0; j <= i; ++j)
dp[j] = triangle[i][j] + min(dp[j],dp[j+1]);
return dp[0];
}
int minimumTotal2(vector<vector<int>>& triangle){
int len = triangle.size();
vector<int> dp(len,0);
for(int i = len - 1; i >= 0; --i){
for(int j = 0; j <= i; ++j){
if(i == len - 1) dp[j] = triangle[i][j];
else dp[j] = triangle[i][j] + min(dp[j],dp[j+1]);
}
}
return dp[0];
}
};
triangle leetcode C++的更多相关文章
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle leetcode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle——LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Triangle LeetCode |My solution
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Triangle leetcode java
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
随机推荐
- pip更新升级和删除包
pip检测更新命令:pip list –outdated pip升级包命令:pip install --upgrade packagename pip卸载包命令:pip uninstall packa ...
- php去除html标签
function cutstr_html($string){ $string = strip_tags($string); $string = preg_replace(["\t" ...
- php在类中使用回调函数 如array_map
<?php class foo { var $var; function bar() { array_map(array($this, "baz"), ar ...
- Shell系列(37)- while和until循环
while循环 只要条件判断式成立则进行循环,并执行循环程序:一旦循环条件不成立,则终止循环 格式 while [ 条件判断式 ] do 程序 done 例子 需求:计算工具,1+2+--100的和 ...
- pandas学习小记
pandas操作整理 导入数据: pd.read_csv(filename):从CSV文件导入数据 pd.read_table(filename):从限定分隔符的文本文件导入数据 pd.read_ex ...
- Git:分布式版本控制系统
参考廖雪峰的 Git 教程:https://www.liaoxuefeng.com/wiki/896043488029600 讲解很详细,这里只做一些个人笔记: 各系统安装 Git :https:/ ...
- GDOI 2021 退役记
Day -n 时常想自己不学OI会怎样,经常畏惧自己其实没有心里想的那样有能力,去机房来麻痹自己 从 3.21 始加大频率刷题,复习以前都学会,而现在都被抛在脑后的算法 反正都要退役了,成绩也得鲜亮点 ...
- Python3入门系列之-----json与字典转换
json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写 JSON 函数 使用 JSON 函数需要导入 json 库:import js ...
- 使用VisualStudioCode开发Vue
前言 本文主要介绍在VisualStudioCode下开发Vue. Nodejs.Npm.Vue的项目搭建参考下面文章. 用后台开发的逻辑理念学习VUE 在Windows下学习Nodejs.Npm和V ...
- salesforce零基础学习(一百零八)MFA
本篇参考:https://security.salesforce.com/mfa https://sfdc.co/bvtuQT (MFA官方研讨会的文档) https://sfdc.co/iwiQK ...