[LeetCode] 120. Triangle _Medium tag: Dynamic Programming
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中的divide and conquer来去计算,此时会有O(2^h) 的复杂度,所以可以加上memorize的array去优化时间复杂度。最后时间复杂度为 O(h^2)
同时通过 f[x][y] = nums[x][y] + min(f[x + 1][y], f[x + 1][y + 1]) 来得到for loop去循环来得到的结果,其实就是上面那种做法用for loop来写而已。时间复杂度为 O(h^2)
1) 利用DFS, Divide and conquer
class Solution:
def triangle(self, nums):
if not nums or len(nums[0]) == 0:
return 0
mem = [[None]*len(nums) for _ in range(len(nums))]
def helper(nums, x, y):
if x == len(nums) - 1:
return nums[x][y]
if mem[x][y] is not None:
return mem[x][y]
left = helper(nums, x + 1, y)
right = helper(nums, x + 1, y + 1)
mem[x][y] = nums[x][y] + min(left, right)
return mem[x][y]
return helper(nums, 0, 0)
2) 利用for loop并且memorize(#using just O(n) space)
more compact
class Solution(object):
def minimumTotal(self, nums):
"""
:type triangle: List[List[int]]
:rtype: int
"""
n = len(nums)
if n == 0: return 0
mem = [[0] * n for _ in range(2)]
for i in range(n - 1, -1, -1):
for j in range(len(nums[i])):
mem[i%2][j] = nums[i][j] if i == n - 1 else nums[i][j] + min(mem[(i + 1)%2][j], mem[(i + 1)%2][j + 1])
return mem[0][0]
[LeetCode] 120. Triangle _Medium tag: Dynamic Programming的更多相关文章
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...
- [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...
- [LeetCode] 256. Paint House_Easy tag: Dynamic Programming
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- 手把手带你使用JS-SDK自定义微信分享效果
https://www.cnblogs.com/backtozero/p/7064247.html
- Spring Boot MongoDB 查询操作 (BasicQuery ,BSON)
MongoDB 查询有四种方式:Query,TextQuery,BasicQuery 和 Bson ,网上太多关于 Query 的查询方式,本文只记录 BasicQuery和Bson 的方式,Basi ...
- xml格式转成json格式,使用Python
import xml.etree.ElementTree root=xml.etree.ElementTree.parse('testXml.xml') book=root.findall('pers ...
- web服务-2、四种方法实现并发服务器-多线程,多进程,协程,(单进程-单线程-非堵塞)
知识点:1.使用多线程,多进程,协程完成web并发服务器 2.单进程-单线程-非堵塞也可以实现并发服务器 1.多进程和协程的代码在下面注释掉的部分,我把三种写在一起了 import socket im ...
- golang map 读写锁与深度拷贝的坑
0X01 golang中,map(字典)无法并发读写 简单来说,新建万条线程对同一个map又读又写,会报错. 为此,最好加锁,其实性能影响并不明显. type taskCache struct{ sy ...
- 监听F5刷新,添加路由前缀
为了解决ninx反向代理,添加路由,但最终react的route还是不认识,研究了半天做个记录: document.addEventListener("keydown",funct ...
- Windows应用程序组成及编程步骤
Windows应用程序组成及编程步骤: 1.应用程序的组成:一个完整的Windows应用程序通常由五种类型的文件组成 1.C语言源程序文件 2.头文件 3.模块定义文件 4.资源描述文件 5.项目文件 ...
- Android Frameworks的base目录内容分析 “Android Frameworks base”
Framework文件夹中base目录下文件分类: Android系统结构框架: Android Framework层各文件夹功能分类:
- jquery复制图片
<div class="img-div"> <a href="javascript:void(0);"><im ...
- bootstrap_开始
bootstrap 一个移动设备优先 UI 库,底层是用 less 写的,依赖于 jQuery. 面试点: bootstrap 的所有盒子都是怪异盒子模型(box-sizing: border-box ...