【LeetCode】120. Triangle 解题报告(Python)
【LeetCode】120. Triangle 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址https://leetcode.com/problems/triangle/description/
题目描述:
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.
题目大意
找出一个正三角形从顶到下的最短路径。
解题方法
很多人肯定和我一样想到的是类似于二叉树的DFS,但是二叉树的每个叶子只会遍历一遍,但是这个三角形中,每个位置相当于被上面的两个节点所共有。所以转而用DP求解。
从顶向下的DP会导致元素越来越多,因此不是很方便,看到大神是从下向上的DP做的,很佩服!
使用minpath[k][i]保存从下向上得到的第k层第i个位置的最短路径。那么有:
minpath[k][i] = min( minpath[k+1][i], minpath[k+1][i+1]) + triangle[k][i];
然后可以看出minpath[k][i]只被用到了一次,所以可以变成一维DP:
For the kth level:
minpath[i] = min( minpath[i], minpath[i+1]) + triangle[k][i];
代码里需要注意的是dp的初始化应该是最下面一层,然后从倒数第二层开始遍历;第layer的元素是layer + 1个。
时间复杂度为O(n^2),空间复杂度O(n)。n为三角形高度。
代码如下:
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
n = len(triangle)
dp = triangle[-1]
for layer in range(n - 2, -1, -1):
for i in range(layer + 1):
dp[i] = min(dp[i], dp[i + 1]) + triangle[layer][i]
return dp[0]
参考资料:
https://leetcode.com/problems/triangle/discuss/38730/DP-Solution-for-Triangle
日期
2018 年 9 月 27 日 —— 国庆9天长假就要开始了!
【LeetCode】120. Triangle 解题报告(Python)的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
随机推荐
- shell批量创建用户
#!/bin/bash cat << EOF ************************************************************ 批量添加用户并随机生 ...
- 聚合与分组查询,F与Q查询
from django.db.models import Q 查询书籍名称是python入门或者价是555.55的书 book_queryset = models.Book.objects.filte ...
- 48-Merge Sorted Array
$88. Merge Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 98885 Total Submis ...
- WebRTC视频分辨率设置
前面我们能够打开摄像头.getUserMedia()时会传入参数,在参数里我们可以指定宽高信息.通过宽高参数控制输出的视频分辨率. html 在页面上摆放一些元素,下面是主要部分 <div id ...
- 日常Java 2021/10/11
抽象类 所有对象都是通过类描述的,但不是所有的类都是用来描述对象,就好比抽象类,此类中没有足够的信息描述一个对象. 抽象类不能实例化对象,所以抽象类必须的继承,才可以使用. 抽象方法 Abstract ...
- A Child's History of England.12
Dunstan, Abbot of Glastonbury Abbey, was one of the most sagacious of these monks. He was an ingenio ...
- acknowledge
accord+knowledge. accord好几个意思,knowledge不遑多让,We gotta acknowledge the word acknowledge has many meani ...
- Learning Spark中文版--第三章--RDD编程(2)
Common Transformations and Actions 本章中,我们浏览了Spark中大多数常见的transformation(转换)和action(开工).在包含特定数据类型的RD ...
- 【leetcode】565. Array Nesting
You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...
- HongYun-ui搭建记录
vue项目windows环境初始化 Element-ui使用 vue2 页面路由 vue SCSS 在VUE项目中使用SCSS ,对SCSS的理解和使用(简单明了) vue axios vue coo ...