题目

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. Spring MVC + Thymeleaf

    参考网址: https://www.cnblogs.com/litblank/p/7988689.html 一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应 ...

  2. Android自定义控件练手——简单的时钟

    首先这应该是一个老生常谈的设计了,但是毕竟身为小白的自己都没动手做过,不动手怎么提高自己呢,所以在这梅林沉船闲暇之际,我就把我的设计流程与思路记录下来.首先来看看效果图吧: 如上图就是一个简单并没有美 ...

  3. pat乙级1045

    从左到右扫描时记录扫描到当前下标为止的最大值,如果当前元素大于这个最大值,那么它就大于它左边的所有值.同理,从右到左扫描记录扫描到当前下标为止的最小值,如果当前元素小于这个最大小值,那么它就小于它右边 ...

  4. Android(java)学习笔记98:如何让你的GridView不再滚动

    1. 如何让你的GridView不再滚动: GridView显示不完整的原因是因为,他的外层也套用了一个滑动的控件,这个解决办法是:重写GridView,是控制GridView不能滚动,就是写一个类继 ...

  5. Android(java)学习笔记93:为什么局部内部类只能访问外部类中的 final型的常量

    为什么匿名内部类参数必须为final类型: 1)  从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变 ...

  6. systemd初始化进程(转)

    Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载内核→内核的初始化→启动初始化进程,初始化进程作为系统第一个进程,它需要 ...

  7. Go - 环境安装

    目录 你好,Go语言 环境安装 目录结构 命令 开发工具 学习网址 小结 你好,Go语言 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. 因工作需要,准备入坑,先从环境安装开始 ...

  8. iOS进阶面试题

    1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ // https://github. ...

  9. Mysql操作方法类

    帮助类: using System; using System.Collections.Generic; using System.Data; using System.Linq; using Sys ...

  10. 【Django】使用list对单个或者多个字段求values值

    使用list对values进行求值: 单个字段的输出结果: price_info=list(Book.objects.filter(auth_id='Yu').values('book_price') ...