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 ...
随机推荐
- HDU2647Reward (拓扑排序)
Reward Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w ...
- 关于PHP的方法参数类型约束
在之前的文章PHP方法参数的那点事儿中,我们讲过关于PHP方法参数的一些小技巧.今天,我们带来的是更加深入的研究一下PHP中方法的参数类型. 在PHP5之后,PHP正式引入了方法参数类型约束.也就是如 ...
- Docker系列(3)- 配置阿里云镜像加速
step-1 登录阿里云找到容器服务 step-2 找到镜像加速地址 step-3 配置使用 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon ...
- Jmeter扩展组件开发(10) - 自定义扩展函数助手的开发
CODE package com.functions;import org.apache.jmeter.engine.util.CompoundVariable;import org.apache.j ...
- P1074 [NOIP2009 提高组] 靶形数独
#include<bits/stdc++.h> using namespace std; const int N=10; int a[N][N],ans[N][N],vis[3][N][N ...
- PKI及SSL协议分析PKI及SSL协议分析
任务一:搭建CA服务器 本任务初步了解CA服务器的原理和配置过程.操作都在CA服务器上. 1.远程桌面方式登录到CA服务器,在CMD下查看本机IP地址: 2.安装证书服务 依次点击:"开始& ...
- php--laravel --debug--mac
1>安装debug 一.下载xdebug文件 1.将phpinfo();的信息全部输入网址中的框,得到适配的xdebug版本: 网址:http://xdebug.org/wizard.php 2 ...
- [转]js之this,call,apply用法
(一)关于this 首先关于this我想说一句话,这句话记住了this的用法你也就差不多都能明白了:this指的是当前函数的对象.这句话可能比较绕,我会举出很多例子和这句话呼应的!(看下文)1.首先看 ...
- P6847-[CEOI2019]Magic Tree【dp,线段树合并】
正题 题目链接:https://www.luogu.com.cn/problem/P6847 题目大意 \(n\)个点的一棵树上,每个时刻可以割掉一些边,一些节点上有果实表示如果在\(d_i\)时刻这 ...
- P5137-polynomial【倍增】
正题 题目链接:https://www.luogu.com.cn/problem/P5137 题目大意 \(T\)组数据给出\(n,a,b,p\)求 \[\left(\sum_{0=1}^na^ib^ ...