Problem Link:

http://oj.leetcode.com/problems/triangle/

Let R[][] be a 2D array where R[i][j] (j <= i) is the minimum sum of the path from triangle[0][0] to tirangle[i][j].

We initialize R[0][0] = triangle[0][0], and update R[][] from i = 1 to n-1:

  R[i][0] = triangle[i][0] + R[i-1][0]

  R[i][i] = triangle[i][i] + R[i-1][i-1]

  R[i][j] = triangle[i][i] + min(R[i-1][j-1], R[i-1][j]) for 1 < j < i

After scan all triangle elements, we just return the minimum value of R[n-1][..]

We note that R[i][..] is only related to R[i-1][..], so we do not need keep all rows of R[][]. Instead, we use two arrays of length n, and each time we update one with the other.

The following is a python implementation, where the time complexity is O(n2) and space complexity is O(n).

class Solution:
# @param triangle, a list of lists of integers
# @return an integer
def minimumTotal(self, triangle):
"""
We scan the triangle from the first row to the last row,
and we maintian an array s[0..n-1] where s[i] is the minimum path sum
if we pick i-th number as the path element in the current row
After scan all rows, we return the minimum value of s[].
"""
n = len(triangle)
if n == 0:
return 0
s = [[2**32] * n, [2**32] * n]
s[0][0] = triangle[0][0]
current = 0
row = 1
for i in xrange(1,n):
# Scan the i-th row, whose length is i+1
# Compute the sum reaching the first element of this row
s[1-current][0] = triangle[i][0] + s[current][0]
# Compute the sum reaching the last element of this row
s[1-current][i] = triangle[i][i] + s[current][i-1]
# Compute others
for j in xrange(1,i):
s[1-current][j] = triangle[i][j] + min(s[current][j], s[current][j-1])
# Go to next row and swith s[0] and s[1]
current = 1 - current
# Return the maximum value of S[current]
return min(s[current])

【LeetCode OJ】Triangle的更多相关文章

  1. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  2. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  3. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

  4. 【LeetCode OJ】Pascal's Triangle

    Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...

  5. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  6. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  7. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  8. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  9. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

随机推荐

  1. java对于文件传输时---编码格式的一些设置方法

    - ----转 读文件: BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高效读取. 可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够 ...

  2. ARM堆栈及特殊指令

    ARM7支持四种堆栈模式:满递减(FD).满递增(FA).空递减(ED).空递增(EA) FD:堆栈地址从上往下递减,且指针指向最后一个入栈元素.FA:堆栈地址从下往上递增,且指针指向最后一个入栈元素 ...

  3. Objective-C:Foundation框架-常用类-NSNumber

    NSArray.NSDictionary是不可以存储C语言中的基本数据类型的.NSNumber可以将基本数据类型包装成对象,这样可以间接将基本数据类型存进NSArray.NSDictionary等集合 ...

  4. intel vt-x处于禁用状态下如何处理

    1.首先看你的bios选项里面有没有该选项,如果没有就更新,更新之后还没有,则不支持 2.找到intel Virtualization Technology 将状态改为Enabled  同时找到int ...

  5. 一些qml资料

    qml开发ios应用 http://www.seanyxie.com/qt-qml%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E4%B9%8B%E5%9C%A8ios%E ...

  6. 西天取经第一步——制作自己的HTML5游戏

    废话不说,直入主题:这是一个休闲益智类游戏,与愤怒的小鸟类似采用Box2dWeb引擎.再开发游戏之前,首先我要把Box2dWeb给总结一下方便以后调用 大家可以在http://code.google. ...

  7. 判断一个字符串是否为有效ip地址

    bool f (const char *s) { int s1,s2,s3,s4; ) { return false; } if ((s1 & 0xffffff00) || (s2 & ...

  8. JavaScript去除数组中的重复性

    Array.prototype.unique = function () { var res = [], hash = {}; for (var i = 0, elem; (elem = this[i ...

  9. C-union的使用

    union有两个作用: 1,节约空间,如果一个struct存在两个互斥的变量,则可以把这个struct变成union 2,将同一个内存作为多种解释 代码: #include <iostream& ...

  10. 在同一个页面中加载多个不同的jQuery版本

    <!-- 从谷歌服务器加载jQuery最新版本--> <script type="text/javascript" src="http://ajax.g ...