原题链接在这里:https://leetcode.com/problems/burst-balloons/

题目:

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: 
(1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Given [3, 1, 5, 8]

Return 167

    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

题解:

Let dp[l][r]表示扎破(l, r)范围内所有气球获得的最大硬币数,不含边界.

我们可以想象:最后的剩下一个气球为m的时候,可以获得的分数为 1 * nums[m] * 1.

For all m from l to r, 有 dp[l][r] = max(dp[l][r], dp[l][m] + nums[l] * nums[m] * nums[r]  + dp[m][r]).

dp[l][m] get maximum, burst all ballons between l and m.

dp[m][n] get maximum bust all ballons between m and r.

Now it leaves position l, r and m only. nums[l]*nums[m]*nums[r].

Maintain the maximum.

l与r的跨度k从2开始逐渐增大;

三重循环依次枚举范围跨度k, 左边界l, 中点m, 右边界r = l + k

Time Complexity: O(n^3). Space: O(n^2).

AC Java:

 public class Solution {
public int maxCoins(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int len = nums.length+2;
int [] newNums = new int[len];
for(int i = 1; i<len-1; i++){
newNums[i] = nums[i-1];
}
newNums[0] = 1;
newNums[len-1] = 1; int [][] dp = new int[len][len];
for(int k = 2; k<len; k++){
for(int l = 0; l<len-k; l++){
int r = l+k;
for(int m = l+1; m<r; m++){
dp[l][r] = Math.max(dp[l][r], dp[l][m] + newNums[l]*newNums[m]*newNums[r] +dp[m][r]);
}
}
}
return dp[0][len-1];
}
}

类似Minimum Cost to Merge StonesRemove Boxes.

LeetCode Burst Balloons的更多相关文章

  1. [LeetCode] Burst Balloons (Medium)

    Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...

  2. [LeetCode] Burst Balloons 打气球游戏

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

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

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

  4. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  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 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  7. 动态规划-Burst Balloons

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

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

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

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

随机推荐

  1. BZOJ3898 : 打的士

    设$f_i$表示选择的答案区间左端点为$i$时,区间长度最小是多少. 那么每来一批人的时候,设$nxt$为$i$右边最近的一个可行决策,则$f_i=\max(f_i,nxt-i)$. 注意到$f$的形 ...

  2. Word2Vec源码解析

    Reference:http://blog.csdn.net/itplus/article/details/37969519  (Word2Vec解析(部分有错)) 源码:http://pan.bai ...

  3. html5调用手机相机并压缩、上传

    近日刚做的一个功能,要在app里使用内嵌页面进行图像的上传.从功能上看,原生的实现应该是最好的.毕竟页面上所有的东西都隔着一个浏览器,所有的实现都要依赖浏览器提供的接口,不同的浏览器对接口的实现又有差 ...

  4. Leetcode Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 51nod百度之星2016练习赛

    今天看了看51nod发现有这样一个练习赛,就做了做.因为实力太弱想不出E题,各位神犇勿D. (5.26UPD:E题想粗来了) A 区间交 不难发现若干线段[li,ri]的交就是[max(li),min ...

  6. BZOJ4521: [Cqoi2016]手机号码

    Description 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不 吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号 码单 ...

  7. 使 Excel 规则更容易理解(Oracle Policy Modeling-Make your Excel rules easier to understand)

    有以下几种方法可以使 Excel 规则更容易理解. 在 Excel 工作簿中缩短属性名称 您可以使用图例关键字创建在 Excel 中引用属性的缩写方式.可以根据情况指定此缩写形式. 要指定图例关键字, ...

  8. iOS 上线被拒收集

    根据上线被拒的原因 自己 也在慢慢总结  希望对各位有所帮助 1)QQ 微信 等第三方平台 必须要做是否安装应用的检测

  9. nginx 中 PHP 调用PEAR.php遇到的问题

    公司有个老项目,写了很多年了,是在apache 上面跑的,无意间,我想让它跑到nginx上,结果遇到了PEAR.php的问题,先安装pear 基本安富有就是 wget http://pear.php. ...

  10. python基础学习二——第二天

    对于python而言,一切事物都是对象,对象是基于类创建的,对象继承了类的属性,方法等特性 一.int 首先我们来查看一下int包含了哪些函数 # python3.x dir(int) # ['__a ...