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. 《Play for Java》学习笔记(六)文件上传file upload

    一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...

  2. lmdb简介——结合MVCC的B+树嵌入式数据库

    lmdb简介 lmdb是openLDAP项目开发的嵌入式(作为一个库嵌入到宿主程序)存储引擎.其主要特性有: 基于文件映射IO(mmap) 基于B+树的key-value接口 基于MVCC(Multi ...

  3. 319. Bulb Switcher——本质:迭代观察,然后找规律

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  4. c++读入之 -- 汉字读入遇到的问题

    好吧,课题和汉语处理有关,于是就要求用c++来读入汉字进行处理. 首先使用wchar_t字符即宽字符,然后这样定义: #include <cstdio> #include <cwch ...

  5. DB2存储过程语法规则

    如何声明一个存储过程CREATE PROCEDURE 存储过程名(IN 输入变量名 输入变量类型,OUT 输出变量名 输出变量类型)紧跟其后的是存储过程属性列表            常用的有:LAN ...

  6. 有关C#中使用if else和try catch的问题及效率问题

    本来if esle 是流程控制 try catch是异常处理,本身其实是没有可比性的,但是开发过程中有的人很容易混用,而且两者有的时候效果似乎一样,所以我还是用了个简单的测试来简单的比较下. 不多说, ...

  7. winform插入sql的事务处理

    实现目的:点击按钮,原子性执行两条sql语句,同时成功或同时失败 数据库有这三个字段(忽略已有的一条记录): 一.用事务的sql语句 按钮事件完整代码: //事务处理插入sql语句 using (SQ ...

  8. [转载]Android View.onMeasure方法的理解

    2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...

  9. [USACO精选] 第三章 排序

    #9 重排干草 2014-02-12 QAQ这么快居然开学了,这么来说时间越来越少了…开学第二天,作业不多,赶紧抽出时间把这道想了很久的题给搞定……真的想了很久,其实看了解题也想了很久,我觉得我等数学 ...

  10. java 基本类库包的作用

    tools.jar:工具类库,它跟我们程序中用到的基础类库没有关系. Jre库包含的jar文件(jdk1.6):resources.jar.rt.jar.jsse.jar.jce.jar.charse ...