Leetcode 312. Burst Balloons

Source: Internet
Author: User

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

Parsing: Typical DP problem, from DP[1][1] iteration of the dp[1][n],n represents the subscript of the last number of arrays, the recurrence relationship is: dp[i][j] = max (Dp[i][k-1] + nums[i-1]*nums[k]*nums[j+1]+dp[k+ 1][J]), here the K range is "I,j", here is the more difficult point is nums[i-1]*nums[k]*nums[j+1] The meaning of this item, why this, if we burst to the last balloon, The last available score is nums[-1]*nums[k]*nums[n], assuming two balloons (if 1 is the optimal K value), dp[1][2] = dp[1][1] + dp[2][2] + nums[1]*nums[2]*1, and so on, The above iteration formula can be summed up. (There is no clear proof of the process, if the great God can give a complete deduction, but also hope to guide the maze)

Code:
classSolution { Public:    intMaxcoins (vector<int>&nums) {         for(intI=0; I<nums.size (); i++){            if(Nums[i] = =0) {nums.erase (Nums.begin ()+H); --i; }        }                Const intn =nums.size (); if(n = =0)return 0; Nums.insert (Nums.begin (),1); Nums.insert (Nums.end (),1); intm =nums.size (); Vector<vector<int>> DP (m,vector<int> (M,0));  for(intk=1; K <= N; k++){             for(ints =1; s+k-1<= N; s++){                intBest =0;  for(intb=0; b<k; b++) { best= Max (best,dp[s][s+b-1]+nums[s-1]*nums[s+b]*nums[s+k]+dp[s+b+1][s+k-1]); } dp[s][s+k-1] =Best ; }        }        returndp[1][n]; }};

Leetcode 312. 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.