题目:

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.

链接: http://leetcode.com/problems/triangle/

题解:

自底向上dp。虽然是个简单题, 但自己现在也能写出一些比较简练的代码了,是进步,要肯定。晚上东方串店撸串去!

Time Complexity - O(n),Space Complexity - O(1)。

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

二刷:

一般来说对于题目给出的数据结构比如Tree或者List<>不要轻易修改。我们另外建立一个数组来dp就好了。这里主要使用了自底向上的思路,先初始化dp数组为triangle的最后一行,再用转移方程dp[i] = triangle.get(i).get(j) + Math.min(dp[i], dp[i + 1])就好了。要注意边界条件。

Java:

Time Complexity - O(n),Space Complexity - O(n)。

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

三刷:

Java:

in-place:

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

Use auxiliary list

public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int min = Integer.MAX_VALUE;
if (triangle == null || triangle.size() == 0) return Integer.MAX_VALUE; List<Integer> res = new ArrayList<>(triangle.get(triangle.size() - 1)); for (int i = triangle.size() - 2; i >= 0; i--) {
for (int j = 0; j < triangle.get(i).size(); j++) {
res.set(j, triangle.get(i).get(j) + Math.min(res.get(j), res.get(j + 1)));
}
}
return res.get(0);
}
}

Reference:

https://leetcode.com/discuss/5337/dp-solution-for-triangle

https://leetcode.com/discuss/10131/my-java-version-solution-with-o-n-space-accepted

https://leetcode.com/discuss/23544/my-8-line-dp-java-code-4-meaningful-lines-with-o-1-space

https://leetcode.com/discuss/20296/bottom-up-5-line-c-solution

120. Triangle的更多相关文章

  1. 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- ...

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

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

  3. LeetCode - 120. Triangle

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

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

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

  5. leetcode 120 Triangle ----- java

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

  6. 【LeetCode】120 - Triangle

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

  7. LeetCode OJ 120. Triangle

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

  8. LeetCode 120. Triangle (三角形)

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

  9. 120. Triangle(中等)

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

随机推荐

  1. WinForm窗体间如何传值的几种方法

    (转) 窗体间传递数据,无论是父窗体操作子窗体,还是子窗体操作符窗体,有以下几种方式: 公共静态变量: 使用共有属性: 使用委托与事件: 通过构造函数把主窗体传递到从窗体中: 一.通过静态变量 特点: ...

  2. document.compatMode的CSS1compat

    document.compatMode BackCompat:标准兼容模式关闭.浏览器宽度:document.body.clientWidth: CSS1Compat:标准兼容模式开启. 浏览器宽度: ...

  3. laravel5.1关于lists函数的bug

    查询语句为: class DateAttrModel extends BaseModel{ -- static function getDays(--){ $days = self::lists('d ...

  4. IP HELPER GetAdaptersAddresses 函数

    自己做的一些笔记,XP以及以后的系统使用: MSDN 函数:http://msdn.microsoft.com/en-US/library/windows/desktop/aa365915(v=vs. ...

  5. vector 内部方法大全 学习(初学者的参考资料)

    1    vector构造函数:也就是如何对一个vector对象进行初始化 ////////////////////////////代码//////////////////////////////// ...

  6. couldnt resolve host mirrorlist.centos

     解决centos 6.3 yum安装软件时找不到镜像问题 [root@nagios-server ~]# yum update –y Loaded plugins: fastestmirror Lo ...

  7. 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证

    http://blog.csdn.net/toss156/article/details/7638529

  8. Who needs an architect?---Martin Fowler

    英文及译文下载链接:http://pan.baidu.com/share/link?shareid=163291504&uk=1428554614 1.文章主题总结 首先我们从文章的几个小标题 ...

  9. css3 简单界面动画

    asdasdasdasda asdasdasdasda

  10. Mybatis+SpringMVC的项目环境搭建

    一.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...