73. Burst balloons

Given n balloons, indexed from 0 to n - 1, each balloon is painted with a number on it represented by an array nums. Burst all the balloons.

If the ith balloon is burst, the coins obtained are nums[i - 1] * nums[i] * nums[i + 1]. If i - 1 or i + 1 goes out of bounds of the array, treat it as if there is a balloon with a 1 painted on it.

Return the maximum coins that can be collected by bursting the balloons wisely.

Example 1:

Input : nums = [3, 1, 5, 8]

Output : 167

Explanation :

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.

Example 2:

Input : nums = [1, 2, 3, 4]

Output : 40

Explanation :

nums = [1, 2, 3, 4] --> [1, 2, 4] --> [1, 4] --> [4] --> []

coins = 2*3*4 + 1*2*4 + 1*1*4 + 1*4*1 = 40.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [1, 5]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 300
  • 1 <= nums[i] <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maxCoins(vector<int>& nums){
//your code goes here
}
};
Test Case

Input:

Nums