原题链接在这里: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. 20145308刘昊阳 《Java程序设计》第4周学习总结

    20145308刘昊阳 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承:面向对象中,为避免多个类间重复定义共同行为使用 把相同代码提升为父 ...

  2. ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】

    Street Lamps   Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...

  3. RMQ问题之ST算法

    RMQ问题之ST算法 RMQ(Range Minimum/Maximum Query)问题,即区间最值问题.给你n个数,a1 , a2 , a3 , ... ,an,求出区间 [ l , r ]的最大 ...

  4. Java_关于App class loader的总结

    Java本身是一种设计的非常简单,非常精巧的语言,所以Java背后的原理也很简单,归结起来就是两点: 1.JVM的内存管理 理解了这一点,所有和对象相关的问题统统都能解决 2.JVM Class Lo ...

  5. Linux软件安装为什么名字不一样

    一.说安装 1.安装 yacc :# yum install byacc 2.安装 glib: :# yum install glibc 3.安装 wireshark :# yum install w ...

  6. 如何在WORD2010中取消自动编号?

    如何在WORD2010中取消自动编号? 使用WORD2010有一个很大的问题就是WORD2010的自动编号问题,WORD2010的自动编号是符合外国人的写作习惯的,对中国人来说不适用. WORD201 ...

  7. Hibernate学习笔记1

    xml文件[封装]写SQL语句<class class='com.briup.user',table='s_emp'> <property name='id' column='id' ...

  8. 清除BOM头源码

    BOM: Byte Order Mark UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支援UTF-16,UTF-32才加上的BOM,BOM签名的意思就是 ...

  9. android基础知识之一

    1:Android系统架构(重点) 分层的架构 JNI java native interface 1.application :应用层 : java 2.application framework ...

  10. bzoj1832: [AHOI2008]聚会--LCA

    本来觉得这是一道挺水的题目,后来觉得出题人挺变态的= = 半个小时敲完后,内存超限它给我看TLE,还是0ms,后来才发现内存限制64m 然后卡了一个小时后AC了.. 题目大意是在一棵树上找三点的最短路 ...