【称号】

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. [Android学习笔记]Unable to execute dex Multiple dex files define:xxxx 问题

    dex filse: Dalvik Execute Files , 即Android虚拟机可执行程序 从字面意思理解是你一个应用中,出现了多个Dex文件定义. 以下情况会出现此错误: 1.你项目中可能 ...

  2. 李兴华JavaWeb开发笔记

    李兴华JavaWeb开发笔记 1.Java语法-基础 环境变量-JAVA_HOME, PATH, ClassPath 变量名 作用 举例 JAVA_HOME 指向JDK目录 C:\Program Fi ...

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

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

  4. BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)

    1367: [Baltic2004]sequence Time Limit: 20 Sec   Memory Limit: 64 MB Submit: 521   Solved: 159 [ Subm ...

  5. Linux内核源代码解析之TCP面向字节流

    本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/11264237 大家都知道TCP是面向stream,而 ...

  6. 【Bug Fix】Error : Can&#39;t create table &#39;moshop_1.#sql-534_185&#39; (errno: 150)

    运行alter操作, alter table xx_shop_info add index FK9050F5D83304CDDC (shop_area), add constraint FK9050F ...

  7. 《windows核心编程系列》十八谈谈windows钩子

    windows应用程序是基于消息驱动的.各种应用程序对各种消息作出响应从而实现各种功能. windows钩子是windows消息处理机制的一个监视点,通过安装钩子能够达到监视指定窗体某种类型的消息的功 ...

  8. Android从raw、assets、SD卡中获取资源文件内容

    先顺带提一下,raw文件夹中的文件会和project一起经过编译,而assets里面的文件不会~~~   另外,SD卡获取文件需要权限哦! //从res文件夹中的raw 文件夹中获取文件并读取数据 p ...

  9. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  10. JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    /** * * @param longitude 经度 * @param latitude 纬度 * @param distance 范围(米) * @return */ public static ...