[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.

考察动态规划,可以想象为一棵树,在构造路径的时候,到达当前节点的路径长度为当前节点的值加上相邻的上层较小值。最直观的是构造一个二维矩阵,自顶向下构造路径,空间复杂度为 O(N^2);为了减少空间复杂度,可以自底向上构造,这样每一次的迭代结果覆盖前一次,最终0号元素就是最短路径值。

思考:自底向上的方法有点归并的感觉,总是将较小的和聚集到一个点。如果要求输出路径上的节点呢?如果底层的存储结果是树呢?

思维导图:

时间: 2024-11-01 01:40:56

[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 database解题记录

175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p left join Address a on p.PersonId=a.PersonId; 7个case耗时1001ms(注意耗时多次提交会不同,一般相差不大,偶尔也有相差大的)   --2015/1/11 176 Second Highest Salary 题目:写一条sql语句查询Employee 

LeetCode: Pascal'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.

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 botto

[leetcode]Triangle @ Python

原题地址:https://oj.leetcode.com/problems/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

LeetCode: Triangle [120]

[题目] 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 

ACdream 1203 - KIDx'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 -- Triangle 路径求最小和( 动态规划问题)

人们常说"细节决定成败". 编码工作中,同样需要关注细节. 本文将给出3个小实例来说明编码中关注细节的重要性,同时给出作者对如何注意编码细节的一点见解(说的不对,请指正). 例1 这个问题如此地显而易见,竟然没有被发现. List<int> numList = new List<int>(); numList.Add(3); numList.Add(1); numList.Add(4); numList.Add(2); numList.Add(5); numLi

[LeetCode] Triangle(&#39;Bottom-up&#39; DP)

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