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

posted @ 2016-05-31 17:56  ElNinoT  阅读(103)  评论(0编辑  收藏  举报