Topic:
Given n
Balloons, indexed from 0
to n-1
. Each balloon are painted with a number on the IT represented by array nums
. You is asked to burst all the balloons. If The burst balloon you'll i
get nums[left] * nums[i] * nums[right]
coins. Here and is left
right
adjacent indices of i
. After the burst, the and then left
right
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]
≤100
Example:
Given[3, 1, 5, 8]
Return167
Nums = [3,1,5,8]--[3,5,8]--[ 3,8]-- [8]-- [] coins = 3*1*5 + * 5*8 + 1*3*8 + 1*8*1 = 167
Links: http://leetcode.com/problems/burst-balloons/
Exercises
Balloon game, hit the balloon after the score is nums[i] * nums[i-1] * nums[i-2], after the left and right side of the balloon connected. The idea of this problem is also very around, see the dietpepsi solution is also very vague, with the sale of stocks with cooldown the same. The method should be with DP or divide and conquer, presumably the idea is that every burst drop a balloon, we do it again 2d DP. The code is not long, so I memorized the answer ... Wipe. Understand to give the two brushes.
Time Complexity-o (n3), Space Complexity-o (n2)
Public classSolution { Public intMaxcoins (int[] orgnums) { if(Orgnums = =NULL|| Orgnums.length = = 0) { return0; } intLen = orgnums.length + 2; int[] Nums =New int[Len]; nums[0] = nums[len-1] = 1;//Boundary for(inti = 0; i < orgnums.length; i++) {nums[i+ 1] =Orgnums[i]; } int[] DP =New int[Len][len]; for(inti = 1; i < Len; i++) {//First balloon for(intLo = 0; Lo < len-i; lo++) {// Left part inthi = Lo + i;//Right part Boundary for(intK = 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]); } } } returnDp[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