您的位置:首页 > 其它

312. Burst Balloons

2016-07-10 14:50 218 查看
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
= 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


摘自
https://discuss.leetcode.com/topic/30746/share-some-analysis-and-explanations http://blog.csdn.net/xyqzki/article/details/50255345 http://bookshadow.com/weblog/2015/11/30/leetcode-burst-balloons/
Be Naive First

When I first get this problem, it is far from dynamic programming to me. I started with the most naive idea the backtracking.

We have n balloons to burst, which mean we have n steps in the game. In the i th step we have n-i balloons to burst, i = 0~n-1. Therefore we are looking at an algorithm of O(n!). Well, it is slow, probably works for n < 12 only.

Of course th
4000
is is not the point to implement it. We need to identify the redundant works we did in it and try to optimize.

Well, we can find that for any balloons left the maxCoins does not depends on the balloons already bursted. This indicate that we can use memorization (top down) or dynamic programming (bottom up) for all the cases from small numbers of balloon until n balloons.
How many cases are there? For k balloons there are C(n, k) cases and for each case it need to scan the k balloons to compare. The sum is quite big still. It is better than O(n!) but worse than O(2^n).

Better idea

We then think can we apply the divide and conquer technique? After all there seems to be many self similar sub problems from the previous analysis.

Well, the nature way to divide the problem is burst one balloon and separate the balloons into 2 sub sections one on the left and one one the right. However, in this problem the left and right become adjacent and have effects on the maxCoins in the future.

Then another interesting idea come up. Which is quite often seen in dp problem analysis. That is reverse thinking. Like I said the coins you get for a balloon does not depend on the balloons already burst. Therefore

instead of divide the problem by the first balloon to burst, we divide the problem by the last balloon to burst.

Why is that? Because only the first and last balloons we are sure of their adjacent balloons before hand!

For the first we have 
nums[i-1]*nums[i]*nums[i+1]
 for the last we have 
nums[-1]*nums[i]*nums
.

OK. Think about n balloons if i is the last one to burst, what now?

We can see that the balloons is again separated into 2 sections. But this time since the balloon i is the last balloon of all to burst, the left and right section now has well defined boundary and do not affect each other! Therefore we can do either recursive
method with memoization or dp.

Final

Here comes the final solutions. Note that we put 2 balloons with 1 as boundaries and also burst all the zero balloons in the first round since they won't give any coins.

The algorithm runs in O(n^3) which can be easily seen from the 3 loops in dp solution.


最初的想法:

     看到这个题目,很显然,第一反应不是用动态规划解决,而是回溯:

     假设现在有n个气球,所以按照题意,若每踩一个气球定义为一个step,则需要n个step才能完成游戏。当进行到第i个step的时候(i<n),还剩下(n-i)个气球,也就是还需要(n-i)step才能完成游戏。用枚举,第一次踩气球,有n种踩法,第2次踩气球有(n-1)中踩法,所以整个游戏有(n!)种完成途径,每个完成途径都可以计算出相应的获得的coin,然后比较一下,取出最大的即可。但是这个算法的复杂度为O(n!),无法接受。所以下面我们逐步优化。

通过观察可以知道。定义:现存的气球集合N,被踩的气球集合M。则 maxcoin(N)和 M 是无关的。也就是已经被踩的气球不会影响到现存的气球的maxcoin计算(这里其实可以看到此问题符合动态规划里面的无后效性,具体可以参考知乎里面一个很好的回答:什么是动态规划?动态规划的意义是什么?)。既然先被踩的气球不会影响后被踩的气球的maxcoin,那我们可以选择先找出被踩两个气球时的maxcoin,被踩三个气球时的maxcoin,......,被踩n个气球时的maxcoin,显然这是一个重叠子问题,并且以上描述显然是一个DP的bottom
up思路。但是,计算被踩k个气球时的maxcoin,需要枚举C(n,k)种情况,并进行比较,这导致子问题过多,也是就是每个递归节点有过多的子节点,增加了计算复杂度,虽然比原始的O(n!)要好一点,但并不优于O(2^n),我们需要寻找具有二项式时间的算法。


更好的想法:

根据前面的分析,该问题可以分解为多个子问题,并逐一解决。于是,我们可以尝试是否可以用分治方法来解决呢?

这里需要明确可以用分治方法解决的问题以及可以用DP解决的问题的异同:

分治和DP都需要将原问题分解成小问题,然后逐一解决;不过分治方法的每个小问题都是不相关的,而DP的子问题则是重叠的(overlapping)。可以参见wiki百科上面的解释:dynamic
programming。

        但是通过前面的分析也知道,之前描述的子问题都是重叠的 (比如你在计算踩K个气球时的maxcoin,肯定会涉及到踩K-1个气球时的结果,这也是可以用bottom up 的意义),因此根本不能用分治方法来求解。自然的一个想法是,我们可不可以先把整体分割,再分别在被分割的各个子整体中用bottom up。这显然是可行的。不过问题在于怎么分割整体,因为整体的分割需要保证各个整体在后面的计算中要保持相互独立性。比如对于[a1,a2,a3,a4,a5,a6,......,an],将分割成两个子整体,分割点为k,则得到
N1 = [a1,a2,a3,....,a(k-1)], N2 = [a(k+1),a(k+2),.....,an]。这里分割点k的意义是踩破了第k个气球。于是把整体分成了两部分,问题在于,根据计算规则,k气球破了之后,a(k-1)和a(k+1)会变成相邻的,如果此时踩a(k-1)或者a(k+1),则都会收到另一个子整体的影响,这样的话,两个子问题就不独立,也就不能用分治了。所以关键的问题在于确定k。

可以发现:

        N1和N2相互独立    <=>  k点为对于整体N的游戏时,最后一个被踩破的气球。

也就是k点被踩破之前,N1和N2重点的气球都不会相互影响。于是我们就成功构造了子问题。因此分治加dp就可以对问题进行求解了。

写一下状态传递方程:

dp[left][right] = max{dp[left][right] , nums[left] * nums[i] * nums[right]  +  nums[left] * nums[i]  +  nums[i] * nums[right]};

以最后被爆破的气球i为界限,把数组分为左右两个子区域

其中 left<i<right , dp[left][right]即为当前子问题:第left和第right之间位置的气球的maxcoin。

Java D&C with Memoization
public int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1;

int[][] memo = new int

;
return burst(memo, nums, 0, n - 1);
}

public int burst(int[][] memo, int[] nums, int left, int right) {
if (left + 1 == right) return 0;
if (memo[left][right] > 0) return memo[left][right];
int ans = 0;
for (int i = left + 1; i < right; ++i)
ans = Math.max(ans, nums[left] * n
b140
ums[i] * nums[right]
+ burst(memo, nums, left, i) + burst(memo, nums, i, right));
memo[left][right] = ans;
return ans;
}
// 12 ms


Java DP
public int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1;

int[][] dp = new int

;
for (int k = 2; k < n; ++k)
for (int left = 0; left < n - k; ++left) {
int right = left + k;
for (int i = left + 1; i < right; ++i)
dp[left][right] = Math.max(dp[left][right],
nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]);
}

return dp[0][n - 1];
}
// 17 ms


-------------------------------------------------------------------

public static int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1;

int[][] dp = new int

;

for(int i=1;i<n-1;i++)
dp[i][i]=nums[i-1]*nums[i]*nums[i+1];

for(int j=1;j<n-1;j++)
for(int i=j-1;i>=1;i--)
for(int k=i;k<=j;k++)
dp[i][j]=Math.max(dp[i][j], nums[i-1]*nums[k]*nums[j+1]+dp[i][k-1]+dp[k+1][j]);

return dp[1][n - 2];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: