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 ...
随机推荐
- python中安装第三方库(使用豆瓣的镜像网站快速安装)
#安装第三方库#标准库,第三方库#pip install selenium 直接从官网进行安装,有时网速会有点慢#可以去国内的镜像站安装#pip install selenium -i https:/ ...
- 255 day03_List、Set、数据结构、Collections
day03 [List.Set.数据结构.Collections] 主要内容 数据结构 List集合 Set集合 Collections 教学目标 [ ] 能够说出List集合特点 [ ] 能够说出常 ...
- 【PHP数据结构】PHP数据结构及算法总结
断断续续地把这个系列写完了,就像上一个设计模式一样,算法这个系列也是前前后后写了将近有一年的时间.当然,都是在业余或者晚上的时间写完的,所以进度如此地慢.更主要的是,既然要写,总得要自己先弄懂吧,对于 ...
- PHP中的MySQLi扩展学习(三)mysqli的基本操作
我们继续 MySQLi 扩展的学习,上篇文章中提到过,MySQLi 的扩展相对于 PDO 来说功能更加的丰富,所以我们依然还会在学习过程中穿插各种 MySQLi 中好玩的方法函数.不过,今天的主角是 ...
- 使用uView UI+UniApp开发微信小程序--微信授权绑定和一键登录系统
在前面随笔<使用uView UI+UniApp开发微信小程序>和<使用uView UI+UniApp开发微信小程序--判断用户是否登录并跳转>介绍了微信小程序的常规登录处理和验 ...
- git 报错 gitThere is no tracking information for the current branch. Please specify which branch you w
新建本地分支后将本地分支推送到远程库, 使用git pull 或者 git push 的时候报错gitThere is no tracking information for the current ...
- html网页乱码
html乱码原因与网页乱码解决方法 html乱码原因与网页乱码解决方法,浏览器浏览网页内容出现乱码符合解决篇(html中文乱码) 造成html网页乱码原因主要是html源代码内中文字内容与html ...
- python读取ini文件
import configparser import os config=configparser.ConfigParser()#创建config对象 file_path=os.path.dirnam ...
- 最小化安装centos7心得
在虚拟机里最小化安装了centos7,只有字符界面,发现网卡不通,解决方法: 调整网卡配置文件: cd /etc/sysconfig/network-scripts/ 有两个ifcfg文件,一个ifc ...
- 基于TLS证书手动部署kubernetes集群
一.简介 Kubernetes是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernetes也叫K8S. K8S是Google内部一个叫Borg的容器集群管理系统 ...