【称号】

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.

【Java代码】

public class Solution {
/* 关键之处在于逆向思维。
* 依据题意会自然而然地想从上而下逐层寻找最优解,可是因为下层元素比上层多。
* 边界处的计算很繁琐。可是假设自下而上,逐层计算到当前层的最优解,那么
* 到达最顶端时。就是所求最优解。
*/
public int minimumTotal(List<List<Integer>> triangle) { //先处理特殊情况
if (triangle == null || triangle.size() == 0) return 0;
if (triangle.size() == 1) return triangle.get(0).get(0); int n = triangle.size();
int[] below = new int[n]; //用于保存下一层的最优解
int[] cur = new int[n]; //用于保存当前层的最优解 int i, j; //初始值为最以下一行的值
List<Integer> lastrow = triangle.get(n - 1);
for (i = 0; i < n; i++) {
below[i] = lastrow.get(i);
} //从倒数第二行開始逐层向上计算
for (i = n - 2; i >= 0; i--) {
List<Integer> row = triangle.get(i); //从底层到当前层每一个位置的最优解取决于其下层临近的两个元素
for (j = 0; j < row.size(); j++) {
if (below[j] < below[j + 1]) cur[j] = below[j] + row.get(j);
else cur[j] = below[j + 1] + row.get(j);
} //层次向上移动,当前层变为下层
for (j = 0; j < row.size(); j++) {
below[j] = cur[j];
}
} return cur[0];
}
}

【扩大】

除了最小输出值加法,如何找到这条道路?

【LeetCode】Triangle 解决报告的更多相关文章

  1. LeetCode: Triangle 解题报告

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

  2. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

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

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

  4. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  5. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  6. 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  7. 【LeetCode】3Sum 解决报告

    这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...

  8. LeetCode 976 Largest Perimeter Triangle 解题报告

    题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  9. LeetCode: Pascal's Triangle 解题报告

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

随机推荐

  1. Qt 的内部进程通信机制

    Qt 的内部进程通信机制 续欣 (xxin76@hotmail.com), 博士.大学讲师 2004 年 4 月 01 日 Qt 作为一种跨平台的基于 C++ 的 GUI 系统,能够提供给用户构造图形 ...

  2. 获取Jenkins project build结果

    当Jenkins管理的build project越来越多的时候,须要脚本收集每一个project的近期一次build结果,从而集中管理.依据业务规则,决定是否重算和何时重算. 以下的命令是利用curl ...

  3. HDU 1498 50 years, 50 colors(最小点覆盖,坑称号)

    50 years, 50 colors Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons ...

  4. Extjs4.2 Desktop 拖动黑色和白色的桌面图标的解决方案

    最近做了一个extjs4.2的desktop桌面demo,该desktop从原来的包中剥离出来,并实现了桌面图标休息,拖动桌面图标,但是,用户抱怨拖动桌面图标会出现黑色和白色,测试,在 extjs4. ...

  5. Conexant声卡实现内录功能(win7)

    Conexant声卡本身没有立体声混音设备可选,所以我们采用virtual audio device,实现内录功能. [1]下载virtual audio device.下载地址:http://dow ...

  6. 祖国版SoloWheel:Airwheel爱尔威火星车 拆箱&上手经验_运动户外_晒物广场_什么值得买

    http://m.baidu.com/from=844b/bd_page_type=1/ssid=0/uid=3151E6C0905477A13653132D762BB6FB/pu=sz%401320 ...

  7. 在阿里云的CentOS环境中安装django

    购买了一台阿里云主机.操作系统为CentOS 6.5.准备在上面跑Django做Web开发.因为CentOS自带的python版本号较低,安装Django先要安装新版本号python.还是费了点周折. ...

  8. Knockout应用开发指南 第七章:Mapping插件

    原文:Knockout应用开发指南 第七章:Mapping插件 Mapping插件 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些 ...

  9. ios7开发者必知

    如果你想为iOS 设备开发app,你需要知道如何与软件交互,如何设计,你还要知道苹果独特的开发理念和开发工具.真正的能力还需要成功地从其他行业领域借鉴核心概念.最后把所有这些东西糅合进你的信息库中, ...

  10. hunnu-11546--Sum of f(x)

    Sum of f(x) Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users:  ...