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 ...
随机推荐
- qt5 上传图片到http服务器
转载请注明出处:http://www.cnblogs.com/dachen408/p/8185060.html qt5 上传图片到http服务器,亲测可以用,qt5.8+vs2015,直接上码; 头文 ...
- SSAS 系列01- DAX公式常用公式
计算第一次购买时间 CALCULATE(FIRSTDATE(FactInternetSales[OrderDate]),ALLEXCEPT(FactInternetSales,FactInternet ...
- (转)用@Resource注解完成属性装配
http://blog.csdn.net/yerenyuan_pku/article/details/52858878 前面我们讲过spring的依赖注入有两种方式: 使用构造器注入. 使用属性set ...
- Android(java)学习笔记183:多媒体之图形颜色的变化
1.相信大家都用过美图秀秀中如下的功能,调整颜色: 2. 下面通过案例说明Android中如何调色: 颜色矩阵 ColorMatrix cm = new ColorMatrix(); paint.se ...
- Python3基础教程(十八)—— 测试
编写测试检验应用程序所有不同的功能.每一个测试集中在一个关注点上验证结果是不是期望的.定期执行测试确保应用程序按预期的工作.当测试覆盖很大的时候,通过运行测试你就有自信确保修改点和新增点不会影响应用程 ...
- Web框架_MVC vs MVT
MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 M表示model,主要用于对数据库层的封装 V表示 ...
- golang结构体排序 - 根据下载时间重命名本地文件
喜M拉Y下载音频到手机,使用ximalaya.exe 解密[.x2m]为[.m4a]根据文件下载创建时间,顺序重命名文件,方便后续播放. 源码如下:package main import ( &quo ...
- Java集合(六)--ArrayList、LinkedList和Vector对比
在前两篇博客,学习了ArrayList和LinkedList的源码,地址在这: Java集合(五)--LinkedList源码解读 Java集合(四)--基于JDK1.8的ArrayList源码解读 ...
- PHP21 MVC
学习目标 MVC设计模式 单一入口机制 MVC的实现 MVC设计模式 Model(模型) 是应用程序中用于处理应用程序数据逻辑的部分.通常模型对象负责在数据库中存取数据. View(视图) 是应用程序 ...
- JSP页面通过c:forEach标签循环遍历List集合
c:forEach>标签有如下属性: 属性 描述 是否必要 默认值items 要被循环的信息 否 无begin 开始的元素(0=第一个元素,1=第二个元素) 否 0end 最后一个元素(0=第一 ...