【Leetcode】Triangle
给定一个由数字组成的三角形,从顶至底找出路径最小和。
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, wheren is the total number of rows in the triangle.
思路一:递归,要求以某个数为起点的最小和,可以先求出以跟它相邻的下一层的两个数为起点的最小和,然后取两者的更小者,最后与该数相加即可。基于此可以写出下面的代码:
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
return minimumTotal(triangle,0,0);
} int minimumTotal(vector<vector<int> > &triangle, int i, int j)
{
if(i == triangle.size()-1)
return triangle[i][j]; int sum0 = minimumTotal(triangle,i+1,j);
int sum1 = minimumTotal(triangle,i+1,j+1); return min(sum0,sum1) + triangle[i][j];
}
};
可以看到代码简洁易懂,不过在Judge large时超时,原因是重复计算了很多子问题,优化它的思路就是用DP,思想是把先把子问题计算好,供查询使用。下面贴上优化的代码:
class Solution
{
public:
int minimumTotal(vector<vector<int> > &triangle)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
// 分配空间
int numRow = triangle.size();
vector<vector<int> > ibuffer;
ibuffer.resize(numRow);
for (int i=0; i<numRow; ++i)
ibuffer[i].resize(numRow); // 从底到顶计算最小和
for (int i=numRow-1; i>=0; --i)
{
vector<int> &row = triangle[i]; for (int j=0; j<row.size(); ++j)
{
if(i==numRow-1)
ibuffer[i][j] = row[j];
else
ibuffer[i][j] = min(row[j]+ibuffer[i+1][j], row[j]+ibuffer[i+1][j+1]);
}
} return ibuffer[0][0];
}
};
上面的代码可以通过Large judge。不过开了一个n*n大小的二维数组,因此空间复杂度为O(n^2),n为三角形的层数。进一步观察发现,开二维数组没有必要,因为每次计算只会查询下一层的计算结果,下下层及更后层的计算结果不会使用到,因此可以只开个大小为n的一维数组就可以了。最终代码如下:
class Solution
{
public:
int minimumTotal(vector<vector<int> > &triangle)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
// 分配空间
int numRow = triangle.size();
vector<int> ibuffer;
ibuffer.resize(numRow); // 从底到顶计算最小和
for (int i=numRow-1; i>=0; --i)
{
vector<int> &row = triangle[i]; for (int j=0; j<row.size(); ++j)
{
if(i==numRow-1)
ibuffer[j] = row[j];
else
ibuffer[j] = min(row[j]+ibuffer[j], row[j]+ibuffer[j+1]);
}
} return ibuffer[0];
}
};
空间复杂度为O(n),n为三角形的层数,时间复杂度为O(K),K为整个三角形中数字的个数。
【Leetcode】Triangle的更多相关文章
- 【LeetCode】Triangle 解决报告
[称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- 【leetcode】Triangle (#120)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- wcf xml消息客户端cookie
public override bool Login(string userName, string password) { using (HttpClient client = new HttpCl ...
- [C#参考]UI和线程(一)
Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程. 什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包 ...
- CSS 与 HTML5 响应式图片
什么是响应式图片? 响应式图片是指:用户代理根据输出设备的分辨率不同加载不同类型的图片,不会造成带宽的浪费.同时,在改变输出设备类型或分辨率时,能及时加载对应类型的图片. CSS3 响应式图片 对于很 ...
- 从零开始PHP学习 - 第三天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- codeforces 416B. Appleman and Tree 树形dp
题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...
- 如何使用picasso 对Android图片下载缓存
相比较其他,picasso的图片缓存更加简单一些,他只需要一行代码就可以表述:导入相关jar包 Picasso.with(context).load("图片路径").into(Im ...
- 使用自定义脚本扩展程序自动执行 VM 自定义任务
在 Build 开发者大会上推出VM 扩展程序的其中一个称为"自定义脚本扩展程序",它支持 PowerShell.如果这是您第一次访问这些博客,可能需要查看以前的博客,请单击 ...
- Windows Azure 自动伸缩已内置
WindowsAzure平台提供的主要优点之一是能够在有需要时快速缩放云中的应用程序以响应波动.去年7月以前,您必须编写自定义脚本或使用其他工具(如Wasabi或MetricsHub)来启用自动 ...
- oralce dubugs
1,The listener supports no services 2,invalid specification for system parameter LOCAL_LISTENER crea ...
- SDOI(队列)
SDOI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Sub ...