题目

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. Android Broadcast Receive

    Broadcast Receive 广播接收(Broadcast Receive)为android的四大组件之一.主要用于监听广播消息,并做出响应.与应用程序中监听事件相比而言,该监听事件为全局监听. ...

  2. 【Linux/Ubuntu学习 11】git查看某个文件的修改历史

    有时候在比对代码时,看到某些改动,但不清楚这个改动的作者和原因,也不知道对应的BUG号,也就是说无从查到这些改动的具体原因了- [注]:某个文件的改动是有限次的,而且每次代码修改的提交都会有commi ...

  3. 使用CSS设置Chrome打印背景色

    以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...

  4. 制作X509证书

    makecert -r -pe -n "CN=XXX" -b 01/01/2005 -e 01/01/2020 -sky exchange -ss my

  5. c++ 输入split

    日期格式为“yyyy/mm/dd”(即年/月/日)格式 scanf("%d/%d/%d", &year, &month, &day);

  6. linux 命令——17 whereis(转)

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和 find相比,whereis查找的速度 ...

  7. Codeforces Round #327 (Div. 2) C Median Smoothing(找规律)

    分析: 三个01组合只有八种情况: 000 s001 s010 0011 s100 s101 1110 s111 s 可以看出只有010,101是不稳定的.其他都是稳定的,且连续地出现了1或0,标记为 ...

  8. Aizu 2456 Usoperanto (贪心)

    贪心,对于一个修饰关系可以连一条有向边,在合并的时候,子节点的序列一定是连续安排的,因为如果有交叉,交换以后一定更优. 然后一个序列一个序列的考虑,长度短的应该在前面,否则同样交换以后更优.因此排序以 ...

  9. vuejs课程简介及框架简介

    vuejs准备知识: 1.前端开发基础 html css js 2.前端模块化基础 3.对es6有初步的了解   vuejs是一种轻量级的MVM框架,他吸收了react和angular的优点,强调re ...

  10. Python实现注册和登录

    一.注册账号需要实现的功能 1.输入:用户名,密码,密码确认 2.限制1:输入的账号和密码不能为空 3.限制2:两次输入密码必须一致 4.限制3:用户名不能重复 5.限制4:错误次数为4次 6.用字典 ...