Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

Note:

  • You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
  • 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Input: [3,1,5,8]
Output: 167
Explanation:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
  coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167

这个题目的思路也是利用 区间Dynamic Programming, 思想跟[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈类似,

动态表达式为:

dp[i][j] 是得到的 [i,j] 里面气球的最大值

for k in [i,j]:

  value_m = ballons[i-1] * ballons[k] * ballons[j+1]

  dp[i][j] = max(dp[i][j], dp[i][k-1] + value_m + dp[k+1][j])

init:

dp[i][i] = ballons[i-1]* ballons[i]* ballons[i+1]

dp[i][j]  = 0 if j < i

1. Constraints

1) size [0, 500]

2) element [0, 100]

2. Ideas

Dynamic Programming      T: O(n^2)    S; O(n^2)

3. Code

class Solution:
def burstBallons(self, nums):
n = len(nums)
ballons = [1] + nums + [1] # plus the corner case
dp = [[0]*(n+2) for _ in range(n+2)] # for corner case
flag = [[0]*(n+2) for _ in range(n+2)]
def helper(l, r):
if flag[l][r]:
return dp[l][r]
if l == r:
dp[l][r] = ballons[l-1] * ballons[l] * ballons[l+1]
elif l < r:
for k in range(l, r+1): # k belongs [l, r], so it is r + 1
value_m = ballons[l-1] * ballons[k] * ballons[r+1]
value_l = helper(l, k-1)
value_r = helper(k+1, r)
dp[l][r] = max(dp[l][r], value_l + value_m + value_r)
flag[l][r] = 1
return dp[l][r]
return helper(1, n)

[LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming的更多相关文章

  1. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  2. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  3. [LeetCode] 312. Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  4. [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  5. LN : leetcode 312 Burst Balloons

    lc 312 Burst Balloons 312 Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is pa ...

  6. [LeetCode] 312. Burst Balloons 爆气球

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  7. [LeetCode] questions conclusion_ Dynamic Programming

    Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...

  8. 动态规划(Dynamic Programming)算法与LC实例的理解

    动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Le ...

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

随机推荐

  1. Linux下常用命令wget的使用技巧

    Linux下wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,尤其对于网络管理员 经常要下载一些软件或从远程服务器恢复备份到本地服务器.如果我们使用虚拟主机,处理这样的 ...

  2. Unity3D笔记 愤怒的小鸟<七> 小鸟群准备动画

    要实现的目标: 1.3只小鸟初始动画 2.完善代码slingShot.js 3.完善代码BirdMoving.js 1.实现3个准备动画:Unity3D内置的动画管理器 1.1.先选择GameObje ...

  3. iOS - viewDidLoad, viewWillDisappear, viewWillAppear区别及加载顺序

    viewWillAppear: Called when the view is about to made visible. Default does nothing视图即将可见时调用.默认情况下不执 ...

  4. 慕课学习--DNS的作用

    因为相对于32位的IP地址,人对域名更加敏感,也更容易记忆.所以一般都是把IP地址转化为域名进行网页的访问. DNS(Domain Name System,域名系统),万维网上作为域名和IP地址相互映 ...

  5. 如何通过python代码解压zip包

    转载至https://www.cnblogs.com/flyhigh1860/p/3884842.html 很多人在Google上不停的找合适自己的压缩,殊不知Py的压缩很不错.可以试试.当然C#,J ...

  6. CBV流程之View源码解析

    CBV是基于反射实现根据请求方式不同,执行不同的方法. 请求流程:view源码解析 1.urls.py :请求一定来执行视图下的as_view方法.也可以直接点击as_view()来找源码. 2.vi ...

  7. ubuntu16.04下安装文献管理工具mendelay

    1.首先下载mendelay的安装包 到官网下载对应版本的安装包,官网地址:Download for Ubuntu and Kubuntu 16.04 LTS, 17.04 and Debian 2. ...

  8. (sklearn)机器学习模型的保存与加载

    需求: 一直写的代码都是从加载数据,模型训练,模型预测,模型评估走出来的,但是实际业务线上咱们肯定不能每次都来训练模型,而是应该将训练好的模型保存下来 ,如果有新数据直接套用模型就行了吧?现在问题就是 ...

  9. hadoop2.x编译安装

    Build instructions for Hadoop (Hadoop 编译安装,参考hadoop源码包中BUILDING.txt文档) ----------------------------- ...

  10. wordpress设置固定链接无效的解决办法

    声明:本人用的是Ubuntu 10.04 LAMP服务 以下内容是针对在Apache服务器下Wordpress修改固定链接出错无效的解决办法: 如果改了固定链接以后出问题,请查看Wordpress根目 ...