[LintCode 109] 数字三角形(Python)
来源:互联网 发布:c语言编程透视 编辑:程序博客网 时间:2024/06/05 16:54
题目描述
给定一个数字三角形,找到从顶部到底部的最小路径和。每一步可以移动到下面一行的相邻数字上。
注意事项
如果你只用额外空间复杂度O(n)的条件下完成可以获得加分,其中n是数字三角形的总行数。
样例
比如,给出下列数字三角形:
[ [2], [3,4], [6,5,7], [4,1,8,3]]
从顶到底部的最小路径和为11 ( 2 + 3 + 5 + 1 = 11)。
思路
自底向上遍历。并用一个辅助列表来记录当前行各点到底部的最小路径和。
代码
class Solution: """ @param: triangle: a list of lists of integers @return: An integer, minimum path sum """ def minimumTotal(self, triangle): # write your code here res = 0 if triangle is None or len(triangle) == 0: return res # 从后往前遍历 # 辅助空间记录当前行各节点到底部的最小值 tmp = triangle[-1] for i in range(len(triangle) - 1, -1, -1): if i + 1 < len(triangle): for j in range(len(triangle[i])): tmp[j] = min(tmp[j], tmp[j + 1]) + triangle[i][j] return tmp[0]
复杂度分析
时间复杂度
阅读全文
0 0
- [LintCode 109] 数字三角形(Python)
- lintcode-数字三角形-109
- LintCode Triangle 数字三角形
- lintcode ----数字三角形
- lintcode triangle 数字三角形
- 数字三角形-LintCode
- lintcode--数字三角形
- 数字三角形-LintCode
- LintCode 数字三角形
- Lintcode-动态规划-数字三角形
- LintCode: 109. 数字三角形 Java
- LintCode做题记录- 数字三角形
- LintCode刷题——数字三角形
- LintCode python 小白3-三角形计数
- LintCode-三角形计数
- lintcode(382)三角形计数
- lintcode 三角形计数
- LintCode之三角形计数
- Java基础(十一)泛型与集合类
- iOS CALayer mask 属性
- 同一服务器下启动多个tomcat
- 数据库 笔试
- mycat学习04之eclipse运行mycat1.6源码
- [LintCode 109] 数字三角形(Python)
- Http协议详解
- python3中easygui
- HDU 6153 拓展kmp
- spark优化之数据倾斜
- hdu 6149 Valley Numer II (状态压缩dp)
- 欢迎使用CSDN-markdown编辑器
- Android OpenGL ES 绘图 -- 缩放、平移、旋转
- Spark之shuffle性能优化