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 inputInput : 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