[leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/
题意:
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.
解题思路:使用动态规划(dp)。需要注意的是从后往前更新!
代码:
class Solution:
# @param triangle, a list of lists of integers
# @return an integer
def minimumTotal(self, triangle):
if len(triangle) == 0: return 0
array = [0 for i in range(len(triangle))]
array[0] = triangle[0][0]
for i in range(1, len(triangle)):
for j in range(len(triangle[i]) - 1, -1, -1):
if j == len(triangle[i]) - 1:
array[j] = array[j-1] + triangle[i][j]
elif j == 0:
array[j] = array[j] + triangle[i][j]
else:
array[j] = min(array[j-1], array[j]) + triangle[i][j]
return min(array)
[leetcode]Triangle @ Python的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- Atom-同步设置
Atom-同步设置 参考博客 CNBLOG First 首先呢,如果我们想要通过Atom的设置和插件的话,我们需要这个一个Atom插件. PS: 因为Gist被墙的原因,所以需要***才行,我挂了ss ...
- 隐马尔科夫模型(HMM)与词性标注问题
一.马尔科夫过程: 在已知目前状态(现在)的条件下,它未来的演变(将来)不依赖于它以往的演变 (过去 ).例如森林中动物头数的变化构成——马尔可夫过程.在现实世界中,有很多过程都是马尔可夫过程,如液体 ...
- qq sid qq sid 是什么 qq sid 怎么用
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha ======= qq sid qq sid 是什么 qq sid 怎么用 ===== ...
- UOJ.26.[IOI2014]Game(交互 思路)
题目链接 \(Description\) 有一张\(n\)个点的图.M每次询问\((u,v)\),你需要回答图中\((u,v)\)间是否有边.如果M可以用\(<n(n-1)/2\)次询问确定图中 ...
- java基础学习总结——super关键字
一.super关键字
- UVALive 6916 Punching Robot dp
Punching Robot 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...
- 深入理解webpack
什么是Webpack WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,TypeScript等),并 ...
- Markdown,你只需要掌握这几个
目录 题记 正文 1. 常用标记 这是一级标题 这是二级标题 这是三级标题 这是高阶标题(效果和一级标题一样 ) 这是次阶标题(效果和二级标题一样) 2. 次常用标记 3. 不常用标记 4. 专项使用 ...
- ios数据保存
- WAP 2.0开发XHTML MP语法及常用功能
XHTML Mobile Profile 的基本结构 <?xml version="1.0" encoding="utf-8"?> <!DOC ...