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的更多相关文章

  1. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming

    基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 基于netty的socket服务端触发了channelInactive方法,但实际连接没有断开的问题

    背景: 一个中小型H5游戏,后端使用基于 netty 的socket服务 服务端 分为 分发服务器 & 业务服务器,业务服务器可负载 用户客户端与分发服务器连接 分发服务器再作为客户端与每台业 ...

  2. git用代码库文件完全覆盖本地/git不能提交jar的设置

    用代码库中的文件完全覆盖本地工作版本. 方法如下: git reset --hard git pull 操作后 本地该目录下所有修改都会被删除!!! 谨慎操作! 项目目录下 文件:.gitignore ...

  3. Coverity代码扫描工具

    1.说明:Coverity代码扫描工具可以扫描java,C/C++等语言,可以和jenkins联动,不过就是要收钱,jenkins上的插件可以用,免费的,适用于小的java项目 2.这是Coverit ...

  4. 阿里云OSS 获取目录下所有文件

    public class AliyunHandle { public static string accessKeyId = "a1uI5xxxxxxxxxrP4H"; publi ...

  5. 【原创】C# war3 巨魔精灵 minimap

    弃坑LOL后,无聊的时候玩玩 war3的RPG地图,巨魔与精灵.  玩了一段时间精灵....然后玩魔结果总是找不到人.所以就有了这个想法. 代码纯粹靠搬运. 说下原理,网上有份代码,可以查看当前选中目 ...

  6. 微服务化不同阶段 Kubernetes 的不同玩法

    本文由  网易云发布. 作为容器集群管理技术竞争的大赢家,Kubernetes 已经和微服务紧密联系,采用 Kubernetes 的企业往往都开始了微服务架构的探索.然而不同企业不同阶段的微服务实践面 ...

  7. 网络编程-Python高级语法-装饰器

    理论:装饰器就是运行一个函数之前首先运行装饰器函数,python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数 ...

  8. Jenkins 快速上手指南

    步骤: 在服务器上安装好jenkins. 在浏览器地址栏输入:服务器地址+":8080/jenkins",回车访问jenkins主页. 单击“新建”,创建任务.如下图: 为任务命名 ...

  9. PostgreSQL自学笔记:8 查询数据

    8 查询数据 8.1 基本查询语句 select语句的基本格式是: select {* | 字段1[,字段2,...]} [ from 表1,表2... [where 表达式] [group by & ...

  10. C_使用clock()函数获取程序执行时间

    clock():捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick ,即“时钟打点”. 常数CLK_TCK:机器时钟每秒所走的时钟打点数. #include & ...