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. javase高级技术 - 泛型

    在写案例之前,先简单回顾下泛型的知识 我们知道,java属于强变量语言,使用变量之前要定义,并且定义一个变量时必须要指明它的数据类型,什么样的数据类型赋给什么样的值. 所谓“泛型”,就是“宽泛的数据类 ...

  2. rbtposeekf的注意事项

    1.发布的odom topic以及 imu topic必须加上协方差部分:2.在发布odom的时候,去掉里面的odom->base_link的tf,因为这个tf会在robot_pose_ekf包 ...

  3. Spring Boot学习笔记:项目开发中规范总结

    Spring Boot在企业开发中使用的很广泛,不同的企业有不同的开发规范和标准.但是有些标准都是一致的. 项目包结构 以下是一个项目常见的包结构 以上是一个项目的基本目录结构,不同的项目结构会有差异 ...

  4. CSS-弹性布局-伪类选择器-复杂选择器

    1.定位 1.堆叠顺序 一旦将元素变为已定位元素的话,元素们则有可能出现堆叠的效果. 如何改变堆叠顺序? 属性:z-index 取值:无单位的数字,数字越大越靠上. 注意: 1.父子元素间,z-ind ...

  5. 58.UIScrollView XIB拖拽约束

    第一步: 拖拽UIScrollView 到控制器上 ,给scrollView 添加约束 ,这时是正常的 第二步:scrollview上添加UIview ,(注意:这个 ScrollView就是根据这个 ...

  6. 2018.06.27 NOIP模拟 节目(支配树+可持久化线段树)

    题目背景 SOURCE:NOIP2015-GDZSJNZX(难) 题目描述 学校一年一度的学生艺术节开始啦!在这次的艺术节上总共有 N 个节目,并且总共也有 N 个舞台供大家表演.其中第 i 个节目的 ...

  7. 4. Father's Impact on a Child's Language Development 父亲对孩子语言发展的影响

    4. Father's Impact on a Child's Language Development 父亲对孩子语言发展的影响 (1)Im families with two working pa ...

  8. centos7 新增ip

    1.进入network-scripts目录:cd /etc/sysconfig/network-scripts/ 2.复制ifcfg-eth0: cp ifcfg-eth0 ifcfg-eth0:0 ...

  9. C++STL deque

    deque双端数组 deque<int> dq; deque<int>::iterator it; dq.push_back();//尾部插入元素 dq.push_front( ...

  10. vim 查找替换批量替换

    一.vi查找:    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢?在vi里可没有菜单-〉查找, 不过没关系,你在命令模式下敲斜杆(/)这时在状态栏(也就是屏幕左下脚)就出 ...