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.

解题思路1:

使用动态规划的思路求解(解法2更符合动态规划思想)。

题目要求只能使用n的额外空间,首先想到了不断迭代数组的方法。

新建包含n个元素的数组A,初始值A[0] = triangle[0][0],即triangle第一层那唯一一个元素的值。

A表示路径运行到当前位置的最小值,之后从第二层到最后一层不断迭代,不断计算到达新一层各个位置的最小值,迭代结束后,返回数组A中记录的最小路径值,即为题目所求。

需要注意的是:

1,因为后一层比前一层多一个元素,所以每层首尾元素在计算时需要特殊对待:

  A[0] = triangle[i][0] + A[0];

  A[i-1] =  triangle[i][i-1] + A[i]

 其他元素公式为:

  A[j] = triangle[i][j] + min(A[j], A[j-1])

2、迭代数组过程,由于新旧值交替运算,很容易出错的地方是,计算一个新值时覆盖了仍需使用的旧值,导致计算其他新值时出错;

 针对本题:

  除了首尾元素外,每次计算一个新值A[j],都需要用到旧的数组中保存的A[j]和A[j-1]的值;

  因此从后往前更新,可以避免出现错误计算。

代码:

 class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int n = triangle.size();
vector<int> path(n);
path[] = triangle[][]; for (int i = ; i < n; ++i) {
path[i] = triangle[i][i] + path[i-];
for (int j = i - ; j > ; --j) {
path[j] = triangle[i][j] + min(path[j], path[j-]);
}
path[] = triangle[i][] + path[];
} int min = path[];
for (int i = ; i < n; ++i){
if (path[i] < min)
min = path[i];
} return min;
}
};

解题思路2:

上一解中存在两个问题:

1、由于从上到下计算,每层比上一层元素多,造成必须单独计算首尾元素,代码丑陋,可读性降低;

  解决:如果从下向上迭代运算,则可以解决这个问题;

2、最终还要遍历数组path,找到最小值返回,增加了时间;

  解决:如果从下向上迭代计算,每次计算都表示从当前位置出发,路径和的最小值,那么迭代到第一层时,就为要求的结果。

新的解法:

数组A初始值设为三角最底一层值,之后由下向上迭代三角。

A[j]的意义为:保存从当前位置运动到三角最底层,所需要的最小路径和;

而每次迭代的意义为:从当前位置,向下选择,选择下一层中,到达底端最小的路径,作为当前位置到达底端的最小路径;

注,和思路一一样,需要在数组迭代中防止新旧值交叉,因为A[j] = triangle[i][j] + min(A[j], A[j+1]),因此从左向右计算新值较为合理。

代码:

 class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int n = triangle.size();
vector<int> path(triangle.back()); for (int i = n - ; i >= ; --i) {
for (int j = ; j <= i; ++j) {
path[j] = triangle[i][j] + min(path[j], path[j+]);
}
} return path[];
}
};

【Leetcode】【Medium】Triangle的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  6. 【leetcode刷题笔记】Triangle

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

  7. 【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, ...

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  10. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

随机推荐

  1. VR内容是如何制作的!

    VR全景视频作为一种新型的视频方式,其震撼效果是毋庸置疑的.目前市场上的VR全景视频也不在少数,越来越多的人能够欣赏到精彩的内容. 首先呢, VR内容场景的呈现分为两种情况: 1.实景拍摄 2.3D建 ...

  2. eclipse中springsource-tool-suite(sts)插件安装教程

    插件的下载参照:http://www.cnblogs.com/jepson6669/p/8540157.html 用过的eclipse不能安装成功,需要重新解压新的才能安装成功,不知道为什么? 解压上 ...

  3. Log4J 配置文件模板及代码说明

    相对而言,这个日志系统的配置就没那么随意了,而且有些功能用起来也不是那么爽,譬如动态读取配置文件.不过鉴于使用这个日志的系统还是很多,所以也写一个demo贴出来,风格跟log4j2一样,配置的说明全在 ...

  4. 关于CSS3动画性能

    前天我去面试了...好吧,对于自己6年6份工作的悲催经历,我自己也是醉了. 但没办法,我这种当时上学没好好学习,临毕业才出家写代码的半吊子码农,起步没起好,以至于一直没能找到真正让自己满意的工作. 通 ...

  5. Hibernate实体类编写规则和主键策略

    一.实体类的编写规则 1.属性要是私有的. 2.要有公开的setter和getter方法供外界访问和修改. 3.每一个实体类要有一个属性作为唯一值(一般都是使用对于数据表的主键). 4.建议数据类型不 ...

  6. c#,利用WPF的ScaleTransform和TranslateTransform实现图片的缩放效果

    一.缩放要求 1.缩放对象在可视区域内,已对象的中心点进行缩放. 2.缩放对象比可视区大,并且对象各边界都在可视区外围,那么已鼠标位置进行缩放. 3.缩放对象比可视区大,但是缩放后某一边界在可视区的对 ...

  7. python-入门教程(操作mysql数据库)

    pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x,而MySQLdb不支持3.x版本. 本文测试python版本:3.6. ...

  8. Java中final修饰参数的作用

    在方法参数前面加final关键字就是为了防止数据在方法体中被修改. 主要分两种情况:第一,用final修饰基本数据类型:第二,用final修饰引用类型. 第一种情况,修饰基本类型(非引用类型).这时参 ...

  9. 使用sqlcmd进行MS-dos方式查询

    在windows选择‘运行’vista需要以管理员身份运行,打开命令提示符窗口 要连接到sql server服务器,必须指定服务器名称,安装命名实例中的,还必须指定实例名.默认情况下,sqlcmd使用 ...

  10. 移除TFS服务器关系

    移除TFS服务器关系:1.将项目和从以前的TFS服务器断开.2.退出VS.3.找到C:\Documents and Settings\Administrator\Local Settings\Appl ...