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

这道题本身不难,它要求只使用O(n)的空间。

对于这一点,不要误解,之前有一题pascal三角的题目就是误解了。

如果限制使用的O(n)的空间,那么很有可能是对一个O(n)的空间进行重复的使用。

这道题要小心的是,对这个O(n)空间进行重复使用的时候,往往从头开始使用时不行的,

此时可以考虑从尾部往前使用,因为第k次循环可能只使用k的空间,那么对于k+1次循环来说,就有

n-k的空间可以利用

时间: 2024-11-01 16:13:21

leetcode Triangle及其思考的相关文章

[LeetCode] Triangle('Bottom-up' 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

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 三角形塔最小路径和

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

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

[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

[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 解题报告

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 Triangle 三角形(最短路)

题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [2], [3,4], [6,5,7], [4,1,8,3] 注意:这里可以2->3>5>1,也可以2->4>5->1,隔层相邻就可以走. 思路:可以从下往上走,也可以从上往下走.都是O(n)的空间,平方阶的复杂度. 从下往上可能更简洁,因为比较到最后只有一个元素,就是为答案了,速度自然也就快,每遍历一层就有1个被淘汰. 然而我一开