Leetcode OJ : Triangle 动态规划 python solution
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的更多相关文章
- leetcode 【 Triangle 】python 实现
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 【LEETCODE OJ】Candy
Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode OJ】Word Break
Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
随机推荐
- 2001: [Hnoi2010]City 城市建设 - BZOJ
DescriptionPS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁.Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费.Louis希望建造最少的 ...
- c++ 链接
header.h #ifndef HEADER_H #define HEADER_H unsigned long getFac(unsigned short num); ; #endif // HEA ...
- ExtJS4.2学习(二)Ext统一组件模型——Panel
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-06/171.html --------------- ...
- js原生代码编写一个鼠标在页面移动坐标的检测功能,兼容各大浏览器
function mousePosition(e) { //IE9以上的浏览器获取 if (e.pageX || e.pageY) { return { ...
- js根据id、pid把数据转为树结构
//格式化树数据 function toTreeData(data) { var pos = {}; var tree = []; var i = 0; while (data.length != 0 ...
- HIBERNATE一对一双向外键联合主键关联
HIBERNATE一对一双向外键联合主键关联: 一. 创建主键类:这个主键必须实现serializedable接口和重写其中的hashCode方法和equals方法:为主键类添加一个叫做@Embedd ...
- 斯坦福数据挖掘Introduction
感谢敖山.薛霄老师把我引进了统计学和现代服务业的大门.......至少是长见识了. 查相似项检索时发现的. 中间一部分资料来自厦门大学数据库实验室,感谢大牛们的传道授业,爱你们. 查资料时发现很多计算 ...
- Educational Codeforces Round 5 A
Problem A:http://codeforces.com/contest/616/problem/A A. Comparing Two Long Integers 果然还是我太天真了(长整数比较 ...
- GameAdmin
username:root e-mail :123@qq.com password:123
- win8系统中PL/SQL Developer连接Oracle出现的问题
注意:所有软件最后不要安装在program files (x86)下 PL/SQL Developer显示Not logged on 以管理员的身份打开PL/SQL Developer 2. t ...