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. HTML5自学笔记[ 1 ]新增标签

    新增语义化标签 <header></header>: 用于页面或板块头部. <footer></footer>:用于页面底部. <nav>& ...

  2. (11)lambda表达式用法

    * lambda 简述    lambda只是一个表达式,函数体比def简单很多    lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去.    lam ...

  3. hibernate annotation注解方式来处理映射关系

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...

  4. postgres 正则表达式

    PostgreSQL正则表达式 基础: Operator Description Example ~ Matches regular expression, case sensitive 'thoma ...

  5. uva 1629

    1629 - Cake slicing Time limit: 3.000 seconds A rectangular cake with a grid of m * n <tex2html_v ...

  6. ZooKeeper(3.4.5) 使用Curator监听事件

    转载:http://www.mamicode.com/info-detail-494364.html 标签: ZooKeeper原生的API支持通过注册Watcher来进行事件监听,但是Watcher ...

  7. MVP Community Camp 社区大课堂

    MVP Community Camp 社区大课堂         微软技术社区大课堂开课啦!!!#MVPComCamp# 全中国微软最有价值专家MVP 在 3月21日周五全天齐聚北京国际会议中心为您呈 ...

  8. java.io.FileOutputStream类的5个构造方法

    java.io.FileOutputStream的构造函数: ①FileOutputStream(File file) ②FileOutputStream(String name) ③FileOutp ...

  9. POJ 3249 拓扑排序+DP

    貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...

  10. node.js+WebStorm路径问题

    目录路径 :A文件夹下有B.C文件夹和app.js文件.B文件夹下有webserver.js文件等. A B webserver.js ...... C ...... app.js WebStorm配 ...