题目

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

代码:oj测试通过 Runtime: 82 ms

 class Solution:
# @param triangle, a list of lists of integers
# @return an integer
def minimumTotal(self, triangle):
# special case
if len(triangle)==0:
return 0
# dp visit
LEVEL = len(triangle)
dp = [0 for i in range(LEVEL)]
for i in range(LEVEL):
for j in range(len(triangle[i])-1,-1,-1):
if j==len(triangle[i])-1 :
dp[j] = dp[j-1] + triangle[i][j]
elif j==0 :
dp[0] = dp[0] + triangle[i][0]
else:
dp[j] = min(dp[j-1],dp[j]) + triangle[i][j]
return min(dp)

思路

典型的动态规划,思路跟Unique Path类似,详情见

http://www.cnblogs.com/xbf9xbf/p/4250359.html

另,这道题还有一个bonus,如何用尽量少的额外空间。

一般的dp思路是,定义一个O(n)的空间,跟三角形等大小的额外空间。

这里只用三角形最底层那一层的大小的空间。

遍历第i层时,利用 dp[1:len(triangle[i])] 的空间存储开始节点到第i层各个节点的最小和。

这里注意:从后往前遍历可以节省数组空间。这是Array的一个常见操作技巧,详情见

http://www.cnblogs.com/xbf9xbf/p/4240257.html

连续刷刷题,能把前后的技巧多关联起来,对代码的能力提升有一定的帮助。

leetcode 【 Triangle 】python 实现的更多相关文章

  1. [leetcode]Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...

  2. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  10. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强

    本文转自:http://www.trinea.cn/android/preloaddatacache/ 本文主要介绍一个支持自动向前或向后获取新数据的缓存的使用及功能.Android图片内存缓存可见I ...

  2. Jquery里面的$(this)和this, 有什么区别

    当你用的是jquery时,就用$(this),如果是JS,就用this $(this).html( $(this).html() + " BAM! "); 这个里的html()是J ...

  3. 卸载gitlab

    一.停止gitlab sudo gitlab-ctl stop 二.卸载gitlab sudo rpm -e gitlab-ce三.查看gitlab进程 杀掉第一个守护进程 kill -9 4473 ...

  4. MVC 控制器向View传值的三种方法(转)

    1.提供视图模型对象 你能把一个对象作为View方法的参数传递给视图. public ViewResult Index() { DateTime date = DateTime.Now; return ...

  5. String和string

    String和string的区别 从位置讲:         1.String是.NET   Framework里面的String,小写的string是C#语言中的string 2.如果把using ...

  6. hdu-1233 还是畅通工程---MST模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 题目大意: 求MST最小生成树 解题思路: Prim算法直接套即可 #include<b ...

  7. squid如何屏蔽User-Agent为空的请求

    搞定了,反过来就行了acl has_user_agent browser ^ http_access deny !has_user_agent

  8. python基础一 day17 作业

    # 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao','nezha']# def func(item):# r ...

  9. kubernetes-控制器Deployment和DaemonSet(八)

    Pod与controllers的关系 •controllers:在集群上管理和运行容器的对象•通过label-selector相关联•Pod通过控制器实现应用的运维,如伸缩,升级等 控制器又称工作负载 ...

  10. Bootstrap历练实例:警告框(Alert)插件事件

    事件 下表列出了警告框(Alert)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 close.bs.alert 当调用 close 实例方法时立即触发该事件. $('#myal ...