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

  1. [
  2. [2],
  3. [3,4],
  4. [6,5,7],
  5. [4,1,8,3]
  6. ]

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

  1. class Solution:
  2. def triangle(self, nums):
  3. if not nums or len(nums[0]) == 0:
  4. return 0
  5. mem = [[None]*len(nums) for _ in range(len(nums))]
  6. def helper(nums, x, y):
  7. if x == len(nums) - 1:
  8. return nums[x][y]
  9. if mem[x][y] is not None:
  10. return mem[x][y]
  11. left = helper(nums, x + 1, y)
  12. right = helper(nums, x + 1, y + 1)
  13. mem[x][y] = nums[x][y] + min(left, right)
  14. return mem[x][y]
  15. return helper(nums, 0, 0)

2) 利用for loop并且memorize(#using just O(n) space)

more compact

  1. class Solution(object):
  2. def minimumTotal(self, nums):
  3. """
  4. :type triangle: List[List[int]]
  5. :rtype: int
  6. """
  7. n = len(nums)
  8. if n == 0: return 0
  9. mem = [[0] * n for _ in range(2)]
  10. for i in range(n - 1, -1, -1):
  11. for j in range(len(nums[i])):
  12. 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])
  13. 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. SQL Server2012如何导出sql脚本并且还原数据库

    一  备份数据库 1  选择某一个数据库,右键依次选择:任务==>生成脚本: 2  选择要编写脚本的数据库对象,注意此处可以选择特定的数据库对象,我们可以选择我们需要备份的数据表. 3   在当 ...

  2. python3 解析 base64 数据

    在阅读 glTF-Tutorial 教程时遇到了解析 base64 数据的问题. 原始 base64 数据为AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAA ...

  3. python---日常练习

    ##输入a,b,c,d4个整数,计算a+b-c*d的结果 #numa=input('请输入整数:'); #numb=input('请输入整数:'); #numc=input('请输入整数:'); #n ...

  4. Codeforces 1097E. Egor and an RPG game 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1097E.html 题解 首先我们求出 $k = f(n) = \max\{x|\frac{x(x+1)}2 ...

  5. nodejs入门篇之linux版的nodejs简易环境安装部署

    第一步:下载二进制安装包 根据linux的不同版本选择32位或64位,因为我的linux的虚拟机是64位的,所以我选择的是64位二进制安装文件(Linux Binariesx64),可以右键选择在新窗 ...

  6. pycharm中连接公网IP方法

    我们的公网IP可以加到pycharm里面,这样程序跑的时候,在测试过程中就用pycharm直接修改文件,然后在pycharm里面上传,操作更加便捷 在pycharm中找到tool按钮,在菜单栏里面 然 ...

  7. mfc简单框架的分析和原理记录

    由于最近在准备考试,可能博客的更新有点慢(呵,我又为自己的懒惰和拖延找借口,总之有时间就更新) 一.菜单 1.在windows中,菜单资源的识别通过HMENU句柄识别 ,但是这个开发过程比较繁琐 2. ...

  8. python面试必问 知识整理

      一 数据类型 1 数字 整型与浮点型   #整型int 作用:年纪,等级,身份证号,qq号等整型数字相关 定义: age=10 #本质age=int(10) #浮点型float 作用:薪资,身高, ...

  9. bzoj 4842 [Neerc2016]Delight for a Cat 最小费用最大流,线性规划

    题意:有n个小时,对于第i个小时,睡觉的愉悦值为si,打隔膜的愉悦值为ei,同时对于任意一段连续的k小时,必须至少有t1时间在睡觉,t2时间在打隔膜.如果要获得的愉悦值尽 量大,求最大的愉悦值和睡觉还 ...

  10. ns2.34移植leach协议

    运行出现的警告: 1. warning:please use -channel as shown in tcl/ex/wireless-mitf.tcl 因为高版本的NS2支持多信道配置,因此无线节点 ...