Leetcode Burst Balloons

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/burst-balloons/

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      +  3*5*8    +  1*3*8      + 1*8*1   = 167

Exercises

DP title, Dp[l][r] indicates the maximum number of coins obtained by all balloons in the range of Pierce (L, R), without Borders.

We can imagine: when the last remaining balloon is m, you can get a score of nums[-1]*nums[m]*nums[n].

Then M between L, R, with dp[l][r] = max (Dp[l][r], dp[l][m] + nums[l] * nums[m] * Nums[r] + dp[m][r]).

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

The triple loop enumerates in sequence the span K, left boundary L, Midpoint m, right boundary R = L + K

Time Complexity:o (n^3). Space:o (n^2).

AC Java:

1  Public classSolution {2      Public intMaxcoins (int[] nums) {3         if(Nums = =NULL|| Nums.length = = 0){4             return0;5         }6         intLen = nums.length+2;7         int[] Newnums =New int[Len];8          for(inti = 1; i<len-1; i++){9Newnums[i] = nums[i-1];Ten         } OneNewnums[0] = 1; ANewnums[len-1] = 1; -          -         int[] DP =New int[Len][len]; the          for(intK = 2; k<len; k++){ -              for(intL = 0; l<len-k; l++){ -                 intR = L +K; -                  for(intm = l+1; m<r; m++){ +Dp[l][r] = Math.max (Dp[l][r], dp[l][m] + newnums[l]*newnums[m]*newnums[r] +dp[m][r]); -                 } +             } A         } at         returnDp[0][len-1]; -     } -}

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.