Total Accepted: 31557 Total Submissions: 116793

 
 

Given a triangle, find the minimum path sum from top to bottom.

Each step you may move to adjacent numbers on the row below.

[
[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.

简单的动态规划

 import sys

 class Solution:
# @param triangle, a list of lists of integers
# @return an integer
def minimumTotal(self, triangle):
length = len(triangle)
l = [0]
l.extend([ sys.maxint for x in range(length - 1)])
def getLastSum(y, x):
return l[x] if x in range(y+1) else sys.maxint
for y in range(length):
for x in range(y, -1, -1):
l[x] = triangle[y][x] + min(getLastSum(y, x), getLastSum(y, x - 1))
return min(l)

Leetcode OJ : Triangle 动态规划 python solution的更多相关文章

  1. leetcode 【 Triangle 】python 实现

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  2. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  3. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  4. 【LEETCODE OJ】Single Number

    Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...

  5. 【LEETCODE OJ】Candy

    Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...

  6. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

  7. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  8. 【LeetCode OJ】Word Break

    Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...

  9. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

随机推荐

  1. 1025: [SCOI2009]游戏 - BZOJ

    Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ...

  2. MySQL的基本命令

    MySQL的基本命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...

  3. spoj 379

    题是水题  但丫的题目意思太难懂 .......   英语水平  ...... #include <cstdio> #include <cstring> #include &l ...

  4. WinDbg调试DMP格式文件

    前言:WinDbg是微软开发的免费源代码级的调试工具.WinDbg可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件.本文的讨论是在安装了Debugging Tools for Win ...

  5. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  6. poj 1095 Trees Made to Order 卡特兰数

    这题用到了卡特兰数,详情见:http://www.cnblogs.com/jackge/archive/2013/05/19/3086519.html 解体思路详见:http://blog.csdn. ...

  7. java:I/O 一行一行读取和写入

    BufferedReader逐行读取 import java.io.*; class Test { public static void main(String args []){ FileReade ...

  8. C#中的线程(三) 使用多线程

    第三部分:使用多线程 1.  单元模式和Windows Forms 单元模式线程是一个自动线程安全机制, 非常贴近于COM——Microsoft的遗留下的组件对象模型.尽管.NET最大地放弃摆脱了遗留 ...

  9. Servlet个人总结

    netstat -an ——查看端口占用情况 netstat -an ——查看是谁占用了哪个端口 端口被占用之后可以关闭端口占用程序或者在conf/server.xml修改本身使用端口 javac - ...

  10. naotu.baidu.com 非常棒的脑图在线工具

    1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...