【LeetCode】Triangle 解题报告

【题目】

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];
    }
}

【扩展】

除了输出最小值之外,如何找出这条路径?

时间: 2024-10-23 00:11:46

【LeetCode】Triangle 解题报告的相关文章

LeetCode: Triangle 解题报告

Triangle 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

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/add-two-numbers/ 题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

[LeetCode] Triangle 解题记录

题目描述: 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