[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 ...
随机推荐
- 不输入密码执行SUDO命令
假如我们需要用户名nenew执行sudo时不用输入密码 1.打开sudoers: visudo /etc/sudoers 2.在文件的最后一行添加: nenew ALL=(ALL) NOPASS ...
- Xshell 连接 vmware中的CentOS 7
参考内容: Xshell 连接 CentOS 7 与 Ubuntu Server,http://www.linuxidc.com/Linux/2017-03/141333.ht ...
- python输入
(程序是如何输入输出的) 先了解一个概念,什么是函数? 简单来说,函数就是封装了一些功能,到时候只需要写一个函数名字,就可以使用这些功能 input函数,它是输入函数,它可以将用户输入的内容当做“字符 ...
- 2.基于梯度的攻击——FGSM
FGSM原论文地址:https://arxiv.org/abs/1412.6572 1.FGSM的原理 FGSM的全称是Fast Gradient Sign Method(快速梯度下降法),在白盒环境 ...
- 项目sql统计
- 关于linux上部署定时python脚本
遇到的坑: Python脚本中的文件操作,最好都用绝对路径, 文件头上写 #!/usr/local/bin/python3.6 ----------------------------------- ...
- 友元(friend)
1.友元类的关系不能传递和继承 ...待续
- Java 中 AOP —— 探讨其存在价值及实现方式对比
AOP 概述 Aspect-oriented programming(面向切面编程).最广为人知的面向侧面的程序设计语言是由施乐帕洛阿尔托研究中心 (施乐帕克 nb!)开发的AspectJ,该语言可以 ...
- 隐藏用户建立(Powershell)
最近做测试的时候发现,windows server2012 使用Mimikatz是直接抓不到明文密码的,而且,直接创建的账号登陆有时会碰到这个问题: ps:2012抓明文需要HKLM:\SYSTEM\ ...
- 客户端无法加入域,报错:“无法与域‘xxx.com’的Active Directory域控制器(AD DC)链接” 请确保键入的域名正确
1.客户端能不能解析到域名? nslookup 一下域名看看解析到的IP的地址 2.客户端的DNS要指向DC 3.客户端的相关服务,workstation,TCP/IP NetBios Helper, ...