【称号】

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学习笔记]SeekBar的使用

    一.SeekBar滑动条的使用 xml声明: <SeekBar android:id="@+id/seekbar" android:layout_width="20 ...

  2. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  3. C++晋升之dynamic_cast

    danamic_cast 动态类型转换 ----RTTI提供的的操作符 ----动态:在执行阶段 ----类型转换:检測指针或引用类型,true->转换 ----体现价值的地方:用于多态 --- ...

  4. iOS文件保存策略

    Where You Should Put Your App’s Files To prevent the syncing and backup processes on iOS devices fro ...

  5. Wamp环境下配置--Apache虚拟主机

    1.首先打开apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#,启用虚拟主机功能 # Virtual hosts In ...

  6. hdu1428之dfs+spfa

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. [poj 1127]Jack Straws[线段相交][并查集]

    题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...

  8. HDU 4028 The time of a day STL 模拟题

    暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vecto ...

  9. linux下安装cmake和mysql遇到的问题总结

    首先是在安装cmake的过程中遇到的问题: 1.開始使用yum命令安装时,不知道为什么一直不行,然后就准备wget 来先下载压缩包,再手动编译. 因为网络限制,wget不能下载外网的东西一直显示con ...

  10. cf 323A A. Black-and-White Cube 立体构造

    A. Black-and-White Cube time limit per test 1 second memory limit per test 256 megabytes input stand ...