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.

 class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function }
};

答题模板

思路:假设三角形共有n行,题目中看出第i行共有i个元素。从top到bottom,我们只考虑minimum path的最后一步是停在了这n个元素的哪一个上面。用数组min_sum(k)表示最后一行到达第k个元素的最小路径(min_sum总长度为n),然后找出min_sum(k)中最小的元素即可。注意一下自上而下递推min_sum时候的递推公式,代码如下:

 class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function int n = triangle.size();
if ( == n) return ;
if ( == n) return triangle[][]; int *min_sum = new int[n];
for(int i=; i<n;i++)
min_sum[i] = ; min_sum[]=triangle[][];
for(int i=;i<n;i++){
for(int j=triangle[i].size()-; j>=;j--){
if (==j){
min_sum[j] += triangle[i][];
}else if (triangle[i].size()-==j){
min_sum[j] = min_sum[j-]+triangle[i][j];
}else{
min_sum[j] = min(min_sum[j-], min_sum[j])+triangle[i][j];
}
}
} int minTotal = min_sum[];
for(int i=;i<n;i++){
minTotal = min(minTotal, min_sum[i]);
} delete[] min_sum;
return minTotal;
} int min(int a, int b){
return (a>b?b:a);
}
};

Answer

注意:一开始的时候内部j那个循环是正着写,后来发现这样有个问题,就是有可能会使用已经变动的过的值去更新下一列。

[leetcode.com]算法题目 - Triangle的更多相关文章

  1. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  2. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  7. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

  9. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. contenteditable设置元素可编辑

    需求背景 实现一个输入框,高度可以随着输入文字的增加而自动增高 有placeholder,输入为空时,显示placeholder 我们知道可以将div的contenteditable设置伪true,将 ...

  2. 2017/2/10:Manven简介与项目管理(入门)

    1.Maven工程的创建 2.使用Manven manven配置文件主要集中在 http://m.blog.csdn.net/article/details?id=50316383

  3. javascript的use strict(使用严格模式)

    上一篇博文学习变量声明带var和不带的区别.搜索相关的文章. 引出了另一个概念. "use strict" 使用严格模式 对于一个使用者而不是概念研究者,我觉得没有意义争论或者讨论 ...

  4. 验证签名机制——java示例

    简单的验证公钥私钥签名认证: 公钥是对外公开的部分,私钥是不公开的部分,一般在项目开发中公钥是给用户,私钥是存于服务器上,二者中有一个加密,则需要另外一个来解密. 下面是java实现的一个比较简单的示 ...

  5. canvas绘图实现浏览器等待效果

    一:创建画布 <canvas width="600" height="600" id="canvas" style="bor ...

  6. 再读c++primer plus 005

    对象和类: 1.类和结构的唯一区别是结构的默认访问类型是public,而类为private: 2.其定义位于类声明中的函数都将自动成为内联函数,也可以在类声明外定义成员函数,并使其成为内联函数,为此只 ...

  7. centos7 安装网卡

    1.虚拟机测试,先开启命令行 su systemctl set-default multi-user.target reboot 2.编辑网卡 虚拟机网络设置成桥接模式 vi /etc/sysconf ...

  8. springboot深入学习(三)-----docker

    一.spring data思路 spring data使用统一的api来对各种数据库存储技术进行数据访问操作提供了支持,包括oracle.mysql.redis.mongoDB等等.主要是通过spri ...

  9. flask中flash不显示

    需要在html文件中body中加入下列语句 <div class='container'> <div class="row"> {% with messag ...

  10. Redis-环境搭建

    Redis官方不提供Windows版,不过微软开源组织提供了Windows版本的Redis,此处将安装Windows版的Reids,供学习使用. 1.下载Windows版Redis安装包: 安装包地址 ...