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

  1. nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
  2. coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167

https://leetcode.com/problems/burst-balloons/


分治+动态规划。

https://leetcode.com/discuss/72216/share-some-analysis-and-explanations

先是暴力的思路,n个气球一个个踩,剩下n - 1个再按顺序踩,一直到最后,复杂度爆表TLE。

加入分治的思想,踩了一个气球后,数组就分成左右两部分,但问题是左边和右边不是独立的,这样分治结果是错的。

逆向思考,最后只有一个气球的时候,结果的是肯定的,因为这个气球的左右分别是左边界和右边界。

举例来说,数组[A,B,C,D,E,F,G],代表任意数字。

首先去掉所有的零,在头和尾加上两个1表示边界。

最外层循环就是从A到G,代表了最后一个踩的气球。

假设遍历到C这个点,最后要踩C,那么C的值是固定的,为1 * C * 1。

然后考虑两边,左边的是以1和C为边界,求出最大值,右边是以C和1为边界求最大值,如图所示。

递归求出结果。

还要开一个二维数组记录中间结果提高效率。

  1. 1 /**
  2. 2 * @param {number[]} nums
  3. 3 * @return {number}
  4. 4 */
  5. 5 var maxCoins = function(nums) {
  6. 6 var dp = [], i, numArr = [1];
  7. 7 for(i = 0; i <nums.length; i++){
  8. 8 if(nums[i] !== 0) numArr.push(nums[i]);
  9. 9 }
  10. 10 numArr.push(1);
  11. 11 var len = numArr.length;
  12. 12 for(i = 0; i < len; i++){
  13. 13 dp[i] = [];
  14. 14 }
  15. 15 return burstBalloons(1, len - 2);
  16. 16
  17. 17 function burstBalloons(start, end){
  18. 18 if(start > end) return 0;
  19. 19 if(dp[start][end]) return dp[start][end];
  20. 20 var max = -Infinity;
  21. 21 for(var i = start; i <= end; i++){
  22. 22 max = Math.max(max, numArr[start - 1] * numArr[i] * numArr[end + 1] +
  23. 23 burstBalloons(start, i - 1) + burstBalloons(i + 1, end));
  24. 24 }
  25. 25 dp[start][end] = max;
  26. 26 return max;
  27. 27 }
  28. 28 };

动态规划-Burst Balloons的更多相关文章

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

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

  2. [LeetCode] Burst Balloons (Medium)

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

  3. 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 ...

  4. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

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

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

  6. 动态规划-击爆气球 Burst Balloons

    2018-10-03 19:29:43 问题描述: 问题求解: 很有意思的题目,首先想到的是暴力遍历解空间,当然也用到了memo,可惜还是TLE,因为时间复杂度确实有点过高了,应该是O(n!). Ma ...

  7. Burst Balloons(leetcode戳气球,困难)从指数级时间复杂度到多项式级时间复杂度的超详细优化思路(回溯到分治到动态规划)

    这道题目做了两个晚上,发现解题思路的优化过程非常有代表性.文章详细说明了如何从回溯解法改造为分治解法,以及如何由分治解法过渡到动态规划解法.解法的用时从 超时 到 超过 95.6% 提交者,到超过 9 ...

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

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

  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. (原+转)Eclipse中Android调用OpenCv

    大部分都是参考下面的网址,如果感觉看起来不舒服,可以直接查看原网址.最后遇到了一点问题: Description      Resource Path Location   Type E:/~\cod ...

  2. MySQL数据库主从同步安装与配置总结

    MySQL的主从同步是一个很成熟的架构,优点为: ①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力: ②在从主服务器进行备份,避免备份期间影响主服务器服务: ③当主服务器出现问题时 ...

  3. php 联系电话验证(手机和固话)

    $tel='要验证的联系电话'; $isMob="/^1[3-5,8]{1}[0-9]{9}$/"; $isTel="/^([0-9]{3,4}-)?[0-9]{7,8} ...

  4. nginx的请求接收流程(二)

    在ngx_http_process_request_line函数中,解析完请求行之后,如果请求行的uri里面包含了域名部分,则将其保持在请求结构的headers_in成员的server字段,heade ...

  5. 一步一步学python(六) - 抽象

    1.string转数字 import  locale locale . atoi( str ) 2.创建函数 函数是可以调用(可能包含参数),执行某种行为并返回一个值 >>>impo ...

  6. U盘开发之安全U盘

    普通型安全U盘,虚拟KEY和U盘两个设备,由主机软件分别对KEY和U盘进行操作,U盘与上位机采用usb mass storage接口,KEY采用HID接口,两者均无需驱动.也有虚拟成光盘和U盘两个设备 ...

  7. 在Unicode版Inno Setup中使用ISSkin给安装程序添加皮肤

    原文 http://www.cnblogs.com/2356/archive/2009/10/27/1590565.html 在Unicode版Inno Setup中使用ISSkin给安装程序添加皮肤 ...

  8. Excel在任务栏中只显示一个窗口的解决办法

     Excel在任务栏中只显示一个窗口的解决办法  以前朋友遇到过这个问题,这次自己又遇到了,习惯了以前的那种在任务栏中显示全部窗口,方便用Alt+Tab键进行切换. 如果同时打开许多Excel工作簿, ...

  9. Linux进程间通信——使用信号

    一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...

  10. 【C++】大数的+-*/四则运算

    所谓大数,则指数值特别大的数,可能会有99位,100位,远远超过了long long表示的范围. 这样的数作四则运算,需要用到字符串.用字符串通过每一位的字符的四则运算来模拟. 废话少说,上代码: # ...