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 ...
随机推荐
- 详解nginx.conf文件配置项(包括负载均衡)
http://www.cnblogs.com/hsapphire/archive/2010/04/08/1707109.html #运行用户 user nobody nobody; #启动进程 wo ...
- Cocos工程命名规则整理(node部分)
CocosCreator工程内的命名工程节点的命名规则工程内节点是程序调用资源的主要凭证,一套统一的命名方式和结构可以很大程度降低程序使用Cocos工程的难度 CocosCreator工程是由node ...
- oracle的Hint
与优化器模式相关的Hint 1 ALl_ROWS 让优化器启用CBO /*+ all_rows */ 2 first_rows(n) 让优化器启用CBO 模式,而且得到目标sql的执行计 ...
- 关于websocket的代码,实现发送信息和监听信息(前端 后端(node.js))
文件结构 node.js代码 // 需要HTTP 模块来启动服务器和Socket.IOvar http= require('http');var fs = require('fs');// 在8080 ...
- [BZOJ3207]:花神的嘲讽(分块解法)
题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...
- ios operationqueue
http://www.hrchen.com/2013/06/multi-threading-programming-of-ios-part-2/
- 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数
<?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...
- laravel 框架发送邮件
http://www.jb51.net/article/121647.htm https://www.cnblogs.com/yxhblogs/p/5998463.html http://www.ji ...
- xmind 8 安装后启动失败(未提示错误信息)
xmind 8 安装后启动失败 前言 家里的计算机也安装了xmind,启动之后界面显示xmind的启动图标,几秒之后启动图标消失(闪退了),然后留我一脸懵逼.想着卸载了安装一个新的应该没有问题 ...
- 周期性任务计划: Crontab
文中部分内容摘自骏马金龙的博客,查看可点击 1. crontab简述 crontab命令用于周期性的执行任务,想要执行这个命令,需要首先启动crond(守护进程)服务才行 crontab是配置管理cr ...