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)的更多相关文章

  1. 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 ...

  2. poj 1163 The Triangle(dp)

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43993   Accepted: 26553 De ...

  3. POJ 1163 The Triangle DP题解

    寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...

  4. leetcode 【 Triangle 】python 实现

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  5. 1. 线性DP 120. 三角形最小路径和

    经典问题: 120. 三角形最小路径和  https://leetcode-cn.com/problems/triangle/ func minimumTotal(triangle [][]int) ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. Triangle---minimum path sum

    动态规划 class Solution: # @param triangle, a list of lists of integers # @return an integer def minimum ...

  8. 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 ...

  9. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

随机推荐

  1. windows.old文件删除

    在安装完新系统后,会发现C盘下有个windows.old文件夹,大约有个10多G,里面都是对之前系统的一些备份,用于对之前系统恢复时使用,一般一个月后会自动清理,若觉得不会再对系统进行老版本恢复时,又 ...

  2. 分享一个开源的JavaScript统计图表库,40行代码实现专业统计图表

    提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...

  3. C++ 类中的static成员的初始化和特点

    C++ 类中的static成员的初始化和特点 #include <iostream> using namespace std; class Test { public: Test() : ...

  4. Window服务程序(windows service application)如何调试

    服务程序不能通过常规的按F5或F11的方式来进行调试和运行,也无法立即运行一个服务或逐步调试它的代码. 因此,你必须安装并启动你的服务,然后附属(attach)一个Debugger到这个服务的进程上.

  5. Wow64

    翻译自Wikipedia: WoW64 运行在微软平台上,WoW64(Windows 32-bit on Windows 64-bit) 是一个Windows的子操作系统, 它能运行32位的应用,在所 ...

  6. [BZOJ3207]:花神的嘲讽(分块解法)

    题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...

  7. centos nginx uwsgi django

    doc link uwsgi link video link

  8. Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框(转载)

    https://blog.csdn.net/evangel_z/article/details/7332535

  9. 【thinkPHP5实现文件上传】

    上传文件 ThinkPHP5.0对文件上传的支持更加简单. 内置的上传只是上传到本地服务器,上传到远程或者第三方平台的话需要自己扩展. 假设表单代码如下: <form action=" ...

  10. 微信小程序wx.request请求服务器json数据并渲染到页面

    [原文出自]: https://blog.csdn.net/weixin_39927850/article/details/79766259 微信小程序的数据总不能写死吧,肯定是要结合数据库来做数据更 ...