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. sql时间转换函数--备忘

    总是忘记 一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的 ...

  2. nginx使用openssl的证书-泛解析

    一.需求分析 我们公司测试环境,域名更换,原来的*.dev.devbao.cn   变为   *.dev.vdai.cn  ,此处的*表示多个二级域名,导致原来为dev.devbao.cn制作的ssl ...

  3. Submission

    EI: ICIC Express Letters: http://www.icicelb.org/elb/index.html IJICIC: http://www.ijicic.net/ijicic ...

  4. hibernate_@GeneratedValue

    JPA通用策略生成器  通过annotation来映射hibernate实体的,基于annotation的hibernate主键标识为@Id,  其生成规则由@GeneratedValue设定的.这里 ...

  5. 第33届 MPD软件工作坊(南京站)有哪些亮点值得我们参加?

    MPD软件工作坊由msup2010年创办,自创办以来,共吸引了万名的软件从业者到场参与.第33届 MPD软件工作坊(南京站)将于12月17-18日在南京召开,大会报名平台:活动家! 快捷报名通道:ht ...

  6. python 内嵌函数, 闭包, 函数装饰器

    一.  函数内嵌 闭包 在python中,函数可以作为返回值, 可以给变量赋值. 在python中, 内置函数必须被显示的调用, 否则不会执行. #!/usr/bin/env python #-*- ...

  7. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...

  8. centos 7 连接 xshell5

    首先 保证你的centos版本与你选择的linux版本相同. 1.首先查看本机IP和网关 2.在centos7命令行下输入nmtui 进入   Edit a commection 选择Edit 按照刚 ...

  9. 【转】Freemarker输出$和html标签等特殊符号

    原文:http://blog.csdn.net/achilles12345/article/details/41820507 场景:程序员都不喜欢看文档,而更喜欢抄例子.所以,我们把平台组的组件都做成 ...

  10. 郑州尚学堂:链表的C语言如何实现动态内存分配

    一.为什么用动态内存分配 但我们未学习链表的时候,如果要存储数量比较多的同类型或同结构的数据的时候,总是使用一个数组.比如说我们要存储一个班级学生的某科分数,总是定义一个float型(存在0.5分)数 ...