给定一个由数字组成的三角形,从顶至底找出路径最小和。

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

  1. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  2. 【leetcode】Triangle (#120)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

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

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

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. mysql的触发器

    删除触发器 drop TRIGGER 触发器名字; 查找库里面的所有触发器 SELECT * FROM information_schema.`TRIGGERS`;show triggers 触发器语 ...

  2. R与数据分析旧笔记(十二)分类 (支持向量机)

    支持向量机(SVM) 支持向量机(SVM) 问题的提出:最优分离平面(决策边界) 优化目标 决策边界边缘距离最远 数学模型 问题转化为凸优化 拉格朗日乘子法--未知数太多 KKT变换和对偶公式 问题的 ...

  3. css 自适应布局

    转载一篇文章: 自适应网页设计(Responsive Web Design) 作者: 阮一峰 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小 ...

  4. java开发工具比较(16个工具修订版)

    1.JDK (Java Development Kit)Java开发工具集 SUN的Java不仅提了一个丰富的语言和运行环境,而且还提了一个免费的Java开发工具集(JDK).开发人员和最终用户可以利 ...

  5. sonix uvc驱动的加入 RT5350支持H264

    依据sonix提供的驱动,须要在内核下进行配置,以加入到内核或与模块的方式进行编译: 1.makefile中加入驱动的文件夹,尽量保持和原有的一致, obj-$(CONFIG_USB_SN9C102) ...

  6. SDWebImage缓存

    缓存图片方法 [[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey]; 读取缓存 UIImage *myCache ...

  7. XMLHttpRequest对象的使用

    1.首先要创建XMLHttpRequest对象,这个对象是前台与后台进行异步的重要对象,现在的浏览器有很多种,创建 XMLHttpRequest 的方法也不相同,所以为了兼容各种浏览器,在创建XMLH ...

  8. NGUI Button 3中点击事件的触发

    NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定在按钮上,点击按钮触发的方法名必须为OnClick,当 ...

  9. Windows Server 2012 R2 服务器管理器介绍和配置使用

    1. 服务管理器是用于管理系统服务的管理工具.一般常用于windows系统,使用这个工具你可以启动.停止服务:设置服务是自动.手动启动或禁用:查看某个服务的相关信息:设置服务以什么用户启动等等(一般包 ...

  10. WPF 实现的等待效果界面

    这个界面的效果是从WinForm 转变过来,可以实现等待的效果,操作完成以后就自动关掉. 效果图如下 有一个动态的手机等待效果的样式,中间的文字可以自己定义,提供了方法可以修改中间"正在加载 ...