Burst Balloons
1 approach, code in every language
Problem description
You are given n balloons in a row, each painted with a number stored in nums. You will burst all of them, one balloon per step.
When you burst the balloon at index i, you collect nums[left] * nums[i] * nums[right] coins, where left and right are the indices of the balloons still remaining directly beside i. If a neighbor no longer exists because it is off the ends of the row, treat its value as 1 (imagine an implicit balloon of value 1 padding both boundaries).
Return the maximum number of coins you can collect after every balloon has been burst.
Example 1
Input: nums = [3,1,5,8]
Output: 167
Bursting in the order 1, 5, 3, 8 yields 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 15 + 120 + 24 + 8 = 167.
Example 2
Input: nums = [1,5]
Output: 10
Burst 1 first for 1*1*5 = 5, then burst 5 for 1*5*1 = 5, totaling 10.
Constraints
n == nums.length1 <= n <= 3000 <= nums[i] <= 100
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Rail Disc Payout is our own problem on the technique behind Burst Balloons: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free