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. 循序渐进看Java web日志跟踪(1)-Tomcat 日志追踪与配置

    日志,是软件运行过程中,对各类操作中重要信息的记录. 日志跟踪,不管对于怎么样的项目来说,都是非常重要的一部分,它关系到项目后期的维护和排错,起着举足轻重的作用.项目开发过程中,对日志的记录规则,也将 ...

  2. HDU------checksum

    Quicksum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. 我理解的session和cookie

    tomcat管理session 理解tomcat是如何生成session的. 当用户请求到web资源有request.getSession()这段代码的时候,request首先会对请求的url判读是否 ...

  4. edittext设置为密文显示

    et_msg.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);

  5. (转载)html中div使用自动高度

    为什么要使用div标签 1.更多的配置项,那就意味着更灵活,当然,难度也更高: 2.可以方便的容纳其他html标签:     static定位就是不定位,出现在哪里就显示在哪里,这是默认取值,只有在你 ...

  6. hdu1027

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const ...

  7. liunx环境C、C++代码编译链接中间代码主要流程

    一个比较小的问题,可以直接看帖子: http://blog.csdn.net/gengyichao/article/details/6544266

  8. dplyr 数据操作 常用函数(5)

    继续来了解dplyr中的其他有用函数 1.sample() 目的是可以从一个数据框中,随机抽取一些行,然后组成新的数据框. sample_n(tbl, size, replace = FALSE, w ...

  9. dplyr 数据操作 常用函数(1)

    上面介绍完dplyr中,几个主要的操作函数后,我们再进一步了解dplyr中那些函数可能我们会经常要用到. 这里主要根据dplyr包作者的书籍目录来把它列出来. 1.add_rownames 添加行名称 ...

  10. [河南省ACM省赛-第三届] 房间安排 (nyoj 168)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168 分析:找到一天中需要最多的房间即可 #include<iostream> ...