题目:

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

链接: http://leetcode.com/problems/burst-balloons/

题解:

射气球游戏,射中气球以后得分是nums[i] * nums[i - 1] * nums[i - 2],之后左右两边气球相连。 这道题思路也很绕,看了dietpepsi的解答也很模糊,跟买卖股票with cooldown一样。方法应该是用dp或者divide and conquer,大概想法是每burst掉一个气球,我们做一遍2d dp。代码并不长,所以我把答案背下来了...擦。理解交给二刷了。

Time Complexity - O(n3), Space Complexity - O(n2)

public class Solution {
public int maxCoins(int[] orgNums) {
if(orgNums == null || orgNums.length == 0) {
return 0;
}
int len = orgNums.length + 2;
int[] nums = new int[len];
nums[0] = nums[len - 1] = 1; // boundary
for(int i = 0; i < orgNums.length; i++) {
nums[i + 1] = orgNums[i];
}
int[][] dp = new int[len][len]; for(int i = 1; i < len; i++) { // first balloon
for(int lo = 0; lo < len - i; lo++) { // left part
int hi = lo + i; // right part boundary
for(int k = lo + 1; k < hi; k++) {
dp[lo][hi] = Math.max(dp[lo][hi], nums[lo] * nums[k] * nums[hi] + dp[lo][k] + dp[k][hi]);
}
}
} return dp[0][len - 1];
}
}

Reference:

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

https://leetcode.com/discuss/72186/c-dynamic-programming-o-n-3-32-ms-with-comments

https://leetcode.com/discuss/72215/java-dp-solution-with-detailed-explanation-o-n-3

https://leetcode.com/discuss/72683/my-c-code-dp-o-n-3-20ms

https://leetcode.com/discuss/73288/python-dp-n-3-solutions

https://leetcode.com/discuss/72802/share-my-both-dp-and-divide-conquer-solutions

https://leetcode.com/discuss/73924/my-36ms-c-solution

312. Burst Balloons的更多相关文章

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

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

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

  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】312. Burst Balloons

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

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

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

  6. 【LeetCode】312. Burst Balloons 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...

  7. 312 Burst Balloons 戳气球

    现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...

  8. 312. Burst Balloons - LeetCode

    Question https://leetcode.com/problems/burst-balloons/description/ Solution 题目大意是,有4个气球,每个气球上有个数字,现在 ...

  9. [LeetCode] Burst Balloons (Medium)

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

随机推荐

  1. C++中的static关键字(转)

    原出处:http://blog.csdn.net/hackbuteer1/article/details/7487694 C++的static有两种用法:面向过程程序设计中的static和面向对象程序 ...

  2. ubuntu 13.04 163源(亲测可用)

    # deb cdrom:[Ubuntu )]/ trusty main restricted # See http://help.ubuntu.com/community/UpgradeNotes f ...

  3. 【收藏】win7打开word每次提示配置解决办法

    打开“我的电脑”——“ C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller ”——找到一个“ ...

  4. struts1 和 struts2中Action什么时候实例化

    精帖1:http://blog.csdn.net/lfsf802/article/details/7277013 精帖1:http://blog.csdn.net/wmj2003/article/de ...

  5. 14、到底改如何区分android的平板、电视、手机

    在没有出现android电视之前,如果要区分平板和手机有很多种方法: 方法1:看是否有通话功能 public boolean isTabletDevice() { TelephonyManager t ...

  6. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  7. 【BZOJ】【1503】 【NOI2004】郁闷的出纳员

    Splay Splay的模板题吧……妥妥的序列操作= =(好像有段时间没写过这种纯数据结构题了……) /************************************************ ...

  8. 使用NPOI和线程池快速加载EXCEL数据

    private void FilterData() { List<Task> tasks = new List<Task>(); IWorkbook workbook = Cs ...

  9. 重定向 vs output redirect

    http://asawicki.info/files/visual_cpp_redirect.png http://asawicki.info/news_1496_redirecting_output ...

  10. IBatis 常用XML

      <select id="GetInfo"> <![CDATA[ select * from vi_WaterStation ]]> <dynami ...