Leetcode:burst Balloons

Source: Internet
Author: User

Given n Balloons, indexed from 0 to n-1. Each balloon are painted with a number on it represented by array nums. You is asked to burst all the balloons. If The burst balloon I you'll get nums[left] * nums[i] * nums[right] coins. Here left and right is 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) may imagine nums[-1] = nums[n] = 1. They is not real therefore you can not burst them. (2) 0≤n≤500, 0≤nums[i]≤100Example:given [3, 1, 5, 8167    = [3,1,5,8]--[3,5 , 8]--[   3,8]--   [8]--[   ]   =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167

This question first see DIETPEPSI's explanation: https://leetcode.com/discuss/72216/share-some-analysis-and-explanations

Be Naive First

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

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

Of course this isn't the point to implement it. We need to identify the redundant works we do in it and try to optimize.

Well, we can find this for any balloons left the maxcoins does not depends on the balloons already bursted. This indicate, we can use memorization (top down) or dynamic programming (bottom up) in the cases from small numb ERs of balloon until n balloons. How many cases is there? For k balloons There is 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 any there seems to is many self similar sub problems from the previous analysis.

Well, the nature-divide the problem is burst one balloon and separate the balloons into 2 sub-sections one on the L EFT and one one one of the right. However, the problem the left and right become adjacent and has effects on the maxcoins in the future.

Then another interesting idea come up. Which is quite often seen in DP problem analysis. That's 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-burst, we divide the problem by the last balloon to BU RST.

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

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

Ok. Think about N Balloons If I am the last one to burst, what's now?

We can see the balloons are again separated into 2 sections. But this time since the balloon I was the last balloon of all to burst, the left and right sections now have well defined Bou Ndary 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 is easily seen from the 3 loops in DP solution.

State transition equation:

Dp[l][r] = max (Dp[l][r], nums[l] * nums[m] * Nums[r] + dp[l][m] + dp[m][r])

DP[L][R] Indicates the maximum number of coins obtained by all balloons in the range of Pierce (L, R), without Borders ;

The span K of L and R increases gradually from 2 onwards;

The triple loop in turn enumerates the span K, the left boundary L, the midpoint m, the right boundary R = L + K;

Because M is the last remaining balloon in the (l,r) interval, the final coin number is num[l]*nums[m]*nums[r], the left interval (l,m) and the right interval (m,r), which were previously exploded, are coin] and dp[l][m], The recursive style comes out, note that the l,r are outside the border.

Finally, note that the first thing to do is to break the balloon for 0, which is the best practice

1  Public classSolution {2      Public intMaxcoins (int[] nums) {3         int[] arr =New int[Nums.length+2];4         intn = 1;5          for(intnum:nums) {6             if(num > 0) 7arr[n++] =num;8         }9Arr[0] = arr[n++] = 1;Ten         int[] DP =New int[n][n]; One          for(intlen=2; len<=n-1; len++) { A              for(intl=0; l<=n-1-len; l++) { -                 intR = L +Len; -                  for(intm=l+1; m<r; m++) { theDp[l][r] = Math.max (Dp[l][r], arr[l]*arr[r]*arr[m]+dp[l][m]+dp[m][r]); -                 } -             } -         } +         returnDp[0][n-1]; -     } +}

There is also the practice of Divide and conquer: (Not delve into)

1  Public intMaxcoins (int[] inums) {2     int[] Nums =New int[Inums.length + 2];3     intn = 1;4      for(intX:inums)if(x > 0) nums[n++] =x;5Nums[0] = nums[n++] = 1;6 7 8     int[] Memo =New int[n][n];9     returnBurst (memo, nums, 0, n-1);Ten } One  A  Public intBurstint[] Memo,int[] Nums,intLeftintRight ) { -     if(left + 1 = = right)return0; -     if(Memo[left][right] > 0)returnMemo[left][right]; the     intAns = 0; -      for(inti = left + 1; I < right; ++i) -Ans = math.max (ans, nums[left] * nums[i] *Nums[right] -+ Burst (memo, Nums, left, i) +Burst (memo, Nums, I, right)); +Memo[left][right] =ans; -     returnans; +}

Leetcode:burst Balloons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.