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的空间可以利用

相关文章:

  • 2022-03-07
  • 2022-01-18
  • 2021-11-25
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
猜你喜欢
  • 2022-01-02
  • 2021-07-09
  • 2022-03-02
  • 2022-01-31
  • 2021-08-02
  • 2021-12-22
相关资源
相似解决方案