题目

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. WPF学习一:XAML的资源(Resources)结构

    一个初学者,把知识做个积累,如果有不对的地方,还请高手指出,谢谢! 先看一段代码:(下面是以Window WPF进行讲解,如果是Web 的话就把<Window改为<Page 而如果是 Us ...

  2. centos7 初体验

    centos7 https://linux.cn/tag-RHCSA%7CRHCSA.html #/etc/sysconfig/network NETWORKING=yes GATEWAY=192.1 ...

  3. jquery datatable 获取当前分页的数据

    使用jquery datatable 遇到分页分别求和时,找了半天才找到获取当前分页数据的方法,以此总结 var table=$('#example').DataTable( { "pagi ...

  4. Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(1)

    下周一出差宁波了,周六日就折腾点视频: 跟着视频教程开发,不过开发环境换linux,上月找工作,某个吉祥物是松鼠的公司要求用linux开发,没用过的,连面试机会都不给,极其高冷:好吧,咱就试试,用li ...

  5. POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

    题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...

  6. opencv与灰度图

    https://blog.csdn.net/qq_32211827/article/details/56854985 首先,灰度图可以是一个通道存成图片,也可以是3个通道存成图片,3个通道存成图片,其 ...

  7. 8--oop

    oop-Python面向对象 Python的面向对象 面向对象编程 基础 共有私有 继承 组合,Mixin 魔法函数 魔法函数概述 构造类魔法函数 运算类魔法函数 1.面向对象概述(ObjectOri ...

  8. MySQL的入门与使用,sqlyog对数据库,表和数据的管理

    MySQL的入门 1.到mysql官网下载. 2.安装mysql软件(一定要放到英文路径下) 3.使用 验证是否成功 将mySQL的bin路径添加到系统环境变量Path中 打开dos命令窗口 Wind ...

  9. python之列表推导、迭代器、生成器

    http://blog.chinaunix.net/uid-26722078-id-3484197.html 1.列表推导 看几个例子,一切就明白了. #!/usr/bin/python number ...

  10. Javascript的数据类型和转换

    JavaScript 数据类型 在 JavaScript 中有 5 种不同的数据类型: string number boolean object function 3 种对象类型: Object Da ...