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. StarUML2 建模工具全平台破解及license验证简要分析

    破解方法:找到安装目录下的文件:LicenseManagerDomain.js阅读得知,改软件用rsa加密用户信息,换行为用户信息的分隔符修改以下代码,然后打开软件点击菜单栏中的帮助->输入li ...

  2. JS - 常用效果代码库 (四)

    1.首字母大写示例: var value = “一段文本或一个参数”; value = value.toString() return value.charAt(0).toUpperCase() + ...

  3. |和||、&&和&

    |和||.&&和& | : 会检查每一个 条件的真伪,再做“或”运算 ||: 按照条件写的顺序,直到一个为true时,后面的条件则不再检查,直接进入条件 & : 会检查 ...

  4. Clojure学习之defmulti

    1. defmulti 宏defmulti 和defmethod 经常被用在一起来定义 multimethod. 宏defmulti 的参数包括一个方法名以及一个dispatch函数,这个dispat ...

  5. sencha touch 简单的倒计时插件

    效果如图: 代码: Ext.define('ux.label.Countdown', { alternateClassName: 'labelCountdown', extend: 'Ext.Comp ...

  6. 使用iLO远程管理HP系列服务器

    iLO是Integrated Ligths-out的简称,是HP服务器上集成的远程管理端口,它是一组芯片内部集成vxworks嵌入式操作系统,通过一个标准RJ45接口连接到工作环境的交换机.只要将服务 ...

  7. 【BZOJ3328】PYXFIB 数论+矩阵乘法

    [BZOJ3328]PYXFIB Description Input 第一行一个正整数,表示数据组数据 ,接下来T行每行三个正整数N,K,P Output T行,每行输出一个整数,表示结果 Sampl ...

  8. jquery如何让checkbox如何取消勾选

    1.取消勾选 $("checkbox").attr("checked", false); 2.勾选 $("checkbox").attr(& ...

  9. thinkCMF----增删改查操作

    thinkCMF的增删改查基本操作: 一.增加数据 $res = Db::name('form')->insert($data); 示例代码: public function index(){ ...

  10. Docker 利用registry创建私有仓库

    一.Docker-registry镜像 下载地址 官方镜像下载比较慢,因为人品问题一直下载不成功,所以选择了国内的镜像. daocloud:   https://hub.daocloud.io/ 还有 ...