题目:

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) 递归,代码很简单,但超时了

package recursion;

import java.util.ArrayList;
import java.util.List; public class Triangle { public int minimumTotal(List<List<Integer>> triangle) {
int m = triangle.size();
return minPath(triangle, , , m, );
} private int minPath(List<List<Integer>> triangle, int row, int col, int m, int prevTotal) {
if (row >= m) return prevTotal;
prevTotal += triangle.get(row).get(col);
return Math.min(minPath(triangle, row + , col, m, prevTotal), minPath(triangle, row + , col + , m, prevTotal));
} }

2) 从下往上进行扫描

package recursion;

import java.util.ArrayList;
import java.util.List; public class Triangle { public int minimumTotal(List<List<Integer>> triangle) {
int m = triangle.size();
int n = triangle.get(m - 1).size();
int[] res = new int[n + 1];
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < triangle.get(i).size(); ++j) {
res[j] = Math.min(res[j], res[j + 1]) + triangle.get(i).get(j);
}
}
return res[0];
} }

LeetCode - Triangle的更多相关文章

  1. [LeetCode] Triangle 三角形

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

  2. leetcode — triangle

    /** * Source : https://oj.leetcode.com/problems/triangle/ * * * Given a triangle, find the minimum p ...

  3. [leetcode]Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...

  4. LeetCode -- Triangle 路径求最小和( 动态规划问题)

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

  5. leetcode Triangle及其思考

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

  6. LeetCode Triangle 三角形(最短路)

    题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...

  7. leetcode—triangle

    1.题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adj ...

  8. LeetCode: Triangle 解题报告

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

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. 人人都是 DBA(VIII)SQL Server 页存储结构

    当在 SQL Server 数据库中创建一张表时,会在多张系统基础表中插入所创建表的信息,用于管理该表.通过目录视图 sys.tables, sys.columns, sys.indexes 可以查看 ...

  2. 【WEB】初探Spring MVC框架

    Spring MVC框架算是当下比较流行的Java开源框架.但实话实说,做了几年WEB项目,完全没有SpringMVC实战经验,乃至在某些交流场合下被同行严重鄙视“奥特曼”了.“心塞”的同时,只好默默 ...

  3. dojo Provider(script、xhr、iframe)源码解析

    总体结构 dojo/request/script.dojo/request/xhr.dojo/request/iframe这三者是dojo提供的provider.dojo将内部的所有provider构 ...

  4. C# WPF 让你的窗口始终钉在桌面上

    IntPtr hWnd = new WindowInteropHelper(Application.Current.MainWindow).Handle; IntPtr hWndProgMan = F ...

  5. java提高篇(十七)-----异常(二)

          承接上篇博文:java提高篇-----异常(一) 五.自定义异常 Java确实给我们提供了非常多的异常,但是异常体系是不可能预见所有的希望加以报告的错误,所以Java允许我们自定义异常来表 ...

  6. 浅谈Excel开发:七 Excel 自定义任务窗体

    前面花了三篇文章讲解了Excel中的UDF函数,RTD函数和异步UDF函数,这些都是Excel开发中的重中之重.本文现在开始接着第二篇文章的菜单系统开始讲解Excel中可供开发的界面元素,本文要讲解的 ...

  7. 基于Css反射形自触发事件,优化你的延时事件

    昨天听w3ctech分享时候,说道orientationchange在不同OS和版本中,存在兼容问题,很多时候触发时候都没有渲染结束,开发同学一般都是基于setTimeout一段时间之后,在去执行具体 ...

  8. HyperDock,让Mac的窗口飞

    三年前写了一篇文章,介绍了Windows 7下的Aero效果,其实最终是想引出来写Mac OS上能产生类似功能的HyperDock应用程序,可惜这一拖,就拖到连Windows 10都快要发布了.没有关 ...

  9. ios NSLog常见使用

    NSLog常见输出格式 Table 1  Format specifiers supported by the NSString formatting methods and CFString for ...

  10. 老学员的学习感悟 --prince2认证有什么用

    2007年一月,我加入了荷兰Irdeto(中国)有限公司.刚入公司,我就结识了PRINCE2(受控环境下的项目管理),才知道prince2是英国政府在政府项目中使用的项目管理标准.这一标准早已在欧洲广 ...