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] <= 100See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI