【LeetCode OJ】Triangle
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的更多相关文章
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【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 ...
- 【LeetCode OJ】Pascal's Triangle
Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
随机推荐
- HDU----(4291)A Short problem(快速矩阵幂)
A Short problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- NSInternalInconsistencyException: loaded the "XXXView" nib but the view outlet was not set
运行过程中App崩溃,报错如标题. 原因: viewController的view没有绑定xib的view. 解决方法: 在File's Owner中将view与viewController的view ...
- Java 字典排序
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import org.ju ...
- 让ie678支持css一些属性及html标签
昨天写的一个页面,用的css3及html5的一些样式与标签,在ie8下看是没有效果的,然后就在晚上查找了一下如何能让ie8也能实现这些效果. 1.添加respond.js文件,Respond.js让I ...
- MVC中view页面用jquery方法绑定select控件值
var sortid = '@Model.myWorkMatter.WorkMatterSortID'; $("#selectSort").val(sortid); $(" ...
- attachEvent ,addEventListener
if (window.attachEvent) { window.attachEvent("onload", remove); ...
- Java 8的五大开发技巧
转载:http://geek.csdn.net/news/detail/94219 在Java 9发布之前,我们来分享一些Java 8开发技巧,本文翻译自JetBrains高级开发主管Trisha G ...
- asp.net 错误跳转
每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...
- C#语法小用法
数据在存为数据库之前,用JS的encodeURIComponent进行编码,现需要在后台代码中进行解码,实现decodeURIComponent的功能, 如下: HttpUtility.UrlDeco ...
- TelephonyManager对黑名单的管理
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util. ...