[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 ...
随机推荐
- STM32串口空闲中断
串口初始化 #include "usart5.h" vu16 UART5_RX_STA=0; char UART5_RX_BUF[UART5_REC_LEN]; u8 UART5_ ...
- Jmeter性能测试之关联(三)
介绍下性能测试很重要的一个知识点---关联, 很多时候程序会在上一个请求随机生成一串字符串, 作为下一个请求的入参验证点, 其实就是动态的入参, 这个时候就需要用到关联, 常用的关联技术就是正则表达式 ...
- C语言fread/fwrite填坑记
坑的描述 用fwrite把数据写入文件,再用fread读取,发现后半部分的数据可能是错的. 原因:原本要写入文件的数据中,有0x0A,如果用的是文本模式打开的文件流,在windows下0x0A会被转换 ...
- C++运算符重载——类型转换
类型转换函数能够实现把一个类 类型 转换成 基本数据类型(int.float.double.char等) 或者 另一个类 类型. 其定义形式如下,注意不能有返回值,不能有参数,只能返回要转换的数据类型 ...
- react-native项目中集成react-native-camera插件
1. 安装 yarn add react-native-camera 2. 手动关联 (1)在AndroidManifest.xml中添加权限配置 <uses-permission androi ...
- 当linux报 “-bash: fork: 无法分配内存”
“-bash: fork: 无法分配内存”,发现连了好多终端,然后断开了一个终端,然后这边终端可以敲命令了 [root@172.16.31.105 /home/www/test]# free -m ...
- 移动端click事件出现300ms延迟
问题分析: 双击缩放是指手在屏幕上快速点击两次,iOS自带的Safari浏览器会将网页缩放至原始比例.当用户在屏幕上单击某元素时,浏览器会先捕获此处单击,但浏览器不知道用户是要单击链接还是要双击该部分 ...
- PostgreSQL自学笔记:8 查询数据
8 查询数据 8.1 基本查询语句 select语句的基本格式是: select {* | 字段1[,字段2,...]} [ from 表1,表2... [where 表达式] [group by & ...
- (Android UI)Android应用程序中资源:图片、字符串、颜色、布局等
Android系统设计采用代码和布局分离的设计模式,因此在设计Android应用程序时需要遵循该设计模式. “把非代码资源(如图片和字符串常量)和代码分离开来始终是一种很好的做法.”---<An ...
- Hibernate Session对象核心方法
1. 持久化对象的状态: 站在持久化的角度,Hibernate 把对象分为四种状态:持久化状态,临时状态,游离状态,删除状态 Session 的特定方法能使对象从一个状态转到另一个状态 临时对象: 在 ...