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. 转载解决:错误的语法:”XXXX“必须是批处理中仅有的语句

    SQL Server 数据库提示“错误的语法:”XXXX“必须是批处理中仅有的语句”报错的原因分析 解析:批处理必须以 CREATE 语句开始.也就是说一个查询分析器里面只有一个批处理语句才是规范的语 ...

  2. 转 数据库中的 date datetime timestamp的区别

    转 数据库中的 date datetime timestamp的区别 DATETIME, DATE和TIMESTAMP类型是相关的.本文描述他们的特征,他们是如何类似的而又不同的. DATETIME类 ...

  3. c#和数据库技术基础之程序设计集散地-数据库

    数据库,不明觉厉,今天我们就来数据库的神秘面纱 1.使用数据库的必要性 随着互联网技术的高速发展,截止到2013年我国网民的数量已接近6亿,网民数量的增长同事带动网上购物,微博,网络视频等新产业的发展 ...

  4. this和super关键字

    this关键字: 1.引用成员变量 2.通过this(参数列表)调用类的重载的构造方法 3.返回对象的值:使用return this,来返回某个类的引用. super关键字: 1.super是一个引用 ...

  5. 实例化Layout中的布局文件(xml)

    什么是LayoutInflater This class is used to instantiate layout XML file into its corresponding View obje ...

  6. Linux下配置用msmtp和mutt发邮件

    Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...

  7. spring aop编程

    1.AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代 ...

  8. 什么是RAID

    RAID 维基百科,自由的百科全书 关于与「 RAID 」同名的其他主题,详见「 RAID (消歧义) 」. 独立硬盘冗余阵列 ( RAID , R edundant A rray of I ndep ...

  9. java之通过反射,来获得某对象的所有方法(类方法提取器)

    参考Thinging in Java 在编程时, 如果不记得一个类是否有某个方法,或者不知道一个类究竟能做些什么,而又不想通过索引或 类的层次结构去查找jdk文档,这时通过反射的小工具能节省很多时间. ...

  10. Google十大惊人产品

    国外资讯网站BusinessInsider刊文细数了谷歌惊世骇俗的十大产品,范围从无人驾驶汽车到太空电梯再到高空风力发电,每一项都令人无限神往,充满未来感. 以下是谷歌十大惊人产品: 众所周知,谷歌并 ...