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

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. c# Linq及Lamda表达式应用经验之 GroupBy 分组

    示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组后结果: 对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.Da ...

  2. 看日记学git摘要~灰常用心的教程

    看日记学git linux 命令行 cd ls / ls -a clear mkdir rmdir echo "hi, good day" > hi.txt touch he ...

  3. Ceph相关博客、网站(256篇OpenStack博客)

    官网文档: http://docs.ceph.com/docs/master/cephfs/ http://docs.ceph.com/docs/master/cephfs/createfs/   ( ...

  4. 在基类中的析构函数声明为virtual

    #include <iostream> using namespace std; class Father { public: ~Father() { cout << &quo ...

  5. html文件引入其它html文件的几种方法:include方式

    可以在一个html的文件当中读取另一个html文件的内容吗?答案是确定的,而且方法不只一种,在以前我只会使用iframe来引用,后来发现了另外的几种方法,那今天就总结这几种方法让大家参考一下. 1.I ...

  6. asp.net MVC过滤器的用法(转)

    转自:http://www.studyofnet.com/news/257.html APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再 ...

  7. WebApi个人理解概要

    WebApi概要 Global文件的作用: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class MvcApplication : System.We ...

  8. HTML静态网页(框架)

    1.frameset frameset最外层,使用时需要去除body改用frameset. <frameset  rows="100,*" frameborder=" ...

  9. spring4.1.3+springmvc+mybatis3.2.1整合

    注意:这里使用了mybatis3.2.1版本,刚开始用了3.4.1的版本,会报一个很奇怪的错(java.lang.AbstractMethodError: org.mybatis.spring.tra ...

  10. C/C++存储区划分

    一. 在c中分为这几个存储区1.栈 - 由编译器自动分配释放2.堆 - 一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收3.全局区(静态区),全局变量和静态变量的存储是放在一块的,初始化 ...