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 ...
随机推荐
- 原创:四种Linux系统开机启动项优命令超给力超详细详解
老葵花哥哥又开课了 接下来是你们的齐天大圣孙悟空给你们带来的详细版Linux系统开机启动优化四种命令 第一种方法是很正常的 第二种有点难理解 第三种来自我的一个奇思妙想 本文档秉承 不要钱也不要臀部的 ...
- (译)IOS block编程指南 1 介绍
Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...
- js toString() 方法 Number() 方法 等 类型转换
1.1 数字类型转字符串 String() 变量.toString() toString() 方法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 1.2 字符串转数字类型 Num ...
- uva12265 Selling Land
见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...
- CNN完成mnist数据集手写数字识别
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...
- 【lua实战摸索】在b.lua调用a.lua的函数
需要掌握知识: lua table的使用(创建自己函数的表作为函数库) 普通函数的调用:tab.func(tab,参数) 等效于表中函数的调用tab:func(参数) 基本思路: 1.在相同目录下创建 ...
- UITextView与UITextfield的区别
IOS中的UITextView和UITextField都是文本输入控件并都能够调用系统键盘.本次特酷把介绍UITextView和UITextField的区别.简单来说,UITextView和UITex ...
- 【2018 CCPC网络赛】1004 - 费马大定理&数学
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6441 Knowledge Point: 1. 费马大定理:当整数n >2时,关于x, y, z的 ...
- CF161D Distance in Tree 点分治
题目: 输入点数为N一棵树,求树上长度恰好为K的路径个数 分析: 题目的数据范围不是很紧,点分治也可以过,树形dp也可以过.这里采用点分治做法. 我们只需要单开一个类似于桶的数组,跑点分治套路,统计即 ...
- (9) openssl enc(对称加密)
对称加密工具,了解对称加密的原理后就很简单了,原理部分见下文. openssl enc -ciphername [-in filename] [-out filename] [-pa ...