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. triangle元素改变的方法

既然是从上往下移动,我们可以把上一层的元素的值加到下一层的相邻元素上,如果下一层某个元素在上一层中有两个相邻的元素,那个就把这两个元素中较小的那个加到下一层元素上。过程如下:

--->或者 

从最后一行的值中我们可以选出最小的那个值,当然这个过程也可以反着进行,从最后一行向上加,上一层元素从它相邻的两个下层元素中选择较小的那个加到它本身。

2. triangle元素不改变的方法

这个方法的思路与上个方法是相同的,只不过这个方法不会改变三角中的元素,而是维持一个数组来存储这个改变的过程。例如从下往上的遍历过程,先把最后一行赋值给数组,从数组相邻元素中选取较小的那个加上这两个元素在上一层的共同相邻元素,这个新值依旧存储在数组中。这时空间复杂度为O(n)。


【java代码1】从上往下遍历,triangle元素改变

 public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0) return 0;
int triszie = triangle.size(); List<Integer> clist = triangle.get(0);
for(int i = 0; i < triszie - 1; i++){
List<Integer> nlist = triangle.get(i+1);
nlist.set(0, clist.get(0) + nlist.get(0));
for(int j = 1; j <= i; j++){
int value = Math.min(clist.get(j), clist.get(j-1)) + nlist.get(j);
nlist.set(j, value);
}
nlist.set(i+1, clist.get(i) + nlist.get(i+1));
clist = nlist;
} int min = clist.get(0);
for(int i = 1; i < clist.size(); i++){
min = Math.min(min, clist.get(i));
}
return min;
}
}

【java代码2】从下往上,triangle元素不改变

 public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0) return 0;
int triszie = triangle.size();
int[] dp = new int[triszie]; for(int i = triszie - 1; i >= 0; i--){
for(int j = 0; j <= i; j++){
if(i == triszie - 1) dp[j] = triangle.get(i).get(j);
else dp[j] = Math.min(dp[j],dp[j+1]) + triangle.get(i).get(j);
}
}
return dp[0];
}
}

LeetCode OJ 120. Triangle的更多相关文章

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

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

  2. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  3. 【一天一道LeetCode】#120. Triangle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】120 - Triangle

    原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...

  5. [leetcode DP]120. Triangle

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

  6. 【LeetCode】120. Triangle (3 solutions)

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

  7. LeetCode OJ:Triangle(三角形)

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

  8. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  9. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

随机推荐

  1. mysql 异常处理

    --该文章内容通过网络搜索组合, mysql 异常,可以自定义异常,再应用.也可使用系统默认的异常,捕获应用. 一.异常定义: DECLARE condition_name CONDITION FOR ...

  2. ECStore图片存储采用阿里云OSS(图片存储)服务

    主要功能:ECStore图片存储采用阿里云OSS(图片存储)服务   适用版本:ECStore 授权方式:授权域名使用,付费插件 联系方式: QQ 275553385  mail: jimingson ...

  3. PHP静态延迟绑定简单示例

    没怎么用过这个新特性,其实也不算新啦,试试吧,现在静态类的继承很方便了 <?php class A { protected static $def = '123456'; public stat ...

  4. Rosenbrock function

    w测试最优化算法性能可通过其. https://en.wikipedia.org/wiki/Rosenbrock_function https://zh.wikipedia.org/wiki/Rose ...

  5. Jquery 2个数组,去除重复的项目

    长数组(Positions_Check) 短数组(PositionTitlesParent_Check) for (var i = 0; i < PositionTitlesParent_Che ...

  6. Facebook FB.init() status参数的作用

    意思是 status 设为 ture 之后调用 FB.getLoginStatus() 不再产生网络请求,数据已经在 FB.init() 调用的时候被请求回来,缓存住了.

  7. stm32

    GPIO NVIC TIME USART ONE WIRE IIC SPI PWM ADC LCD XPT UCOSiii移植 定时器 蓝牙 陀螺仪

  8. Pelican主题配置:elegant

    简介 elegant是Mac风格的优秀主题,简单,专注文章本身. A responsive, minimal, and stylish theme for Pelican:https://github ...

  9. <poj - 3268> Silver Cow Party 牛のpart 最短路径问题

    本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最 ...

  10. [帖子收集]通用Windows平台(UWP)

    通用Windows平台,universal windows platform,UWP 什么是通用 Windows 平台 (UWP) 应用?(微软MSDN) 如何在通用 Windows 平台应用中使用现 ...