Triangle(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.
代码:空间复杂度O(n2),可以改进。
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int row=triangle.size();
int col=triangle[row-].size();
int dp[row][col];
memset(dp,,sizeof(dp));
int sum1=;int sum2=;int index=;
int res=(~(unsigned))>>;
for (int i = ; i < row; ++i)
{
sum1+=triangle[i][];
sum2+=triangle[i][index];
dp[i][]=sum1;
dp[i][index]=sum2;
++index;
}
for (int i = ; i < row; ++i){
for (int j = ; j < triangle[i].size()-; ++j){
dp[i][j]=min(dp[i-][j],dp[i-][j-])+triangle[i][j];
}
}
for(int i=;i<col;++i){
if(dp[row-][i]<res) res=dp[row-][i];
}
return res;
}
};
改进:因为每次只需上一次的结果,直接在原处覆盖就行,空间复杂度O(n),用temp保留本次结果,并利用上次结果dp,求完本次之后,直接用temp覆盖dp,再进行下一次。
代码:
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
if(triangle.empty())
return ;
int row=triangle.size();
int col=triangle[row-].size();
vector<int> dp(col,);
vector<int> temp(col,);
int sum1=;int sum2=;int index=;
int res=(~(unsigned))>>;
dp[]=triangle[][];
for (int i = ; i < row; ++i){
temp.resize(col,);
for (int j = ; j < triangle[i].size(); ++j){
if(j==)
temp[j]=dp[j]+triangle[i][j];
else if(j==triangle[i].size()-)
temp[j]=dp[j-]+triangle[i][j];
else
temp[j]=min(dp[j-],dp[j])+triangle[i][j];
}
dp=temp;
}
for(int i=;i<col;++i){
if(dp[i]<res) res=dp[i];
}
return res;
}
};
Triangle(dp)的更多相关文章
- nyoj--18--The Triangle(dp水题)
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...
- poj 1163 The Triangle(dp)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43993 Accepted: 26553 De ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- leetcode 【 Triangle 】python 实现
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 1. 线性DP 120. 三角形最小路径和
经典问题: 120. 三角形最小路径和 https://leetcode-cn.com/problems/triangle/ func minimumTotal(triangle [][]int) ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- Triangle---minimum path sum
动态规划 class Solution: # @param triangle, a list of lists of integers # @return an integer def minimum ...
- LeetCode 全解(bug free 训练)
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- POJ1163 The Triangle 【DP】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36918 Accepted: 22117 De ...
随机推荐
- c# 从DataGridVieew导出到excel
public static bool DataGridViewToExcel(DataGridView dataGridView, bool isShowExcel) { int rowsQty = ...
- (四)docker创建私人仓库
(一) 简介 仓库(Repository)是集中存放镜像的地方.仓库可以 被认为是一个具体的项目或目录.例如对于仓库地址 docker.sina.com.cn/centos:centos63 来说,d ...
- TensorFlow低阶API(二)—— 张量
简介 正如名字所示,TensorFlow这一框架定义和运行涉及张量的计算.张量是对矢量和矩阵向潜在的更高维度的泛化.TensorFlow在内部将张量表示为基本数据类型的n维数组. 在编写TensorF ...
- mybatis-4 mybatis与spring结合使用及原理
1.创建项目maven,方便依赖下载.使用的jar如下: <dependencies> <dependency> <groupId>org.springframew ...
- 谈谈你对java的理解
这个题目是考察多个方面 但是要回答出关键点: 1.平台无关性 2.GC 3.语言特性.泛型.反射.lamda 4.面向对象 5.类库 6.异常处理
- loadClass和forName 的区别
类的加载方式 1.隐式加载:new 2.显式加载,loadClass,forName 等 loadClass 和 forName的区别 类的装载过程 1.加载:通过classLoader加载class ...
- luogu 2709小b的询问--莫队
https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...
- [模板] Exgcd
求解一组ax+bc=gcd(a,b) #include<iostream> #include<cstdio> using namespace std; int exgcd(in ...
- 28. TRIGGERS ,29. USER_PRIVILEGES,30. VIEWS
28. TRIGGERS TRIGGERS表提供有关触发器的信息.要查看有关表的触发器的信息,您必须具有该表的TRIGGER权限. TRIGGERS表有以下列: TRIGGER_CATALOG :触发 ...
- 蓝牙bluez学习(1) Stack Architecture
Bluez支持的features Core Specification 4.2 (GAP, L2CAP, RFCOMM, SDP, GATT) Classic Bluetooth (BR/EDR) B ...