Burst Balloons is LeetCode 312 (Hard). There are n balloons in a row, and balloon i is painted with the number nums[i]. Bursting a balloon pays its number times the numbers of the two balloons still standing next to it at that moment; past either end of the row, count a balloon painted 1. Once a balloon bursts, its two neighbours become next to each other. Burst every balloon and return the most coins you can collect.
There are at most 300 balloons, each painted 0 to 100.
That size allows O(n³) work: about 4.5 million steps.
Choosing the first balloon to burst leads nowhere. Once it pops, its two neighbours touch, so the left part and the right part still affect each other.
Choose the balloon that bursts last instead. Say balloon m is the last one to go between two balloons left and right that are still standing. Everything between them is already gone, so m pays exactly vals[left] * vals[m] * vals[right], whatever order came before.
Two separate halves: while m stands, nothing on its left can ever be next to anything on its right, so each side is its own smaller problem.
One rule for the ends: put a 1 at each end of the row, and the whole row becomes the range between those two pads.
Try every m and keep the best total.
How to spot this pattern
The score of removing an item depends on its current neighbours, and removing it changes who the neighbours are. That is the sign to stop asking what goes first and ask what goes last inside a range: interval DP over pairs of boundaries.
03
Approach
Try it first
Before reading on, take [3, 1, 5, 8] and decide which balloon should burst last. Write down what that last burst pays and what is left to solve on each side of it. Aim for O(n³) time and O(n²) space.
Both fill the same table of ranges in O(n³). The loop version is the one in the steps, code and live demo. The recursive version is closer to how the idea is first said out loud, and its code comes after the demo.
1
Pad the row
Build vals = [1] + nums + [1]. The two pads are never burst. They stand for "nothing there, count 1", so a balloon at the edge of the row pays by the same rule as one in the middle.
2
Define one cell
dp[left][right] is the most coins from bursting every balloon strictly between left and right while those two stay standing. When right = left + 1 nothing lies between them, so the cell is 0.
3
Try each balloon as the last one
For every m between left and right, the total is dp[left][m] + vals[left] * vals[m] * vals[right] + dp[m][right]. Keep the largest. The two side cells cover smaller ranges, and m keeps them apart until it bursts.
4
Why no order is missed
Any order of bursting a range has some balloon that goes last. If that is m, its coins split into the final burst plus what each side earned on its own, and each side can earn at most its own cell. So the best m matches the best order, and every total tried is a real order.
5
Fill short ranges first
Loop over the gap right - left from 2 up to n - 1. Every side range used in a total has a smaller gap, so it is already filled. The answer is dp[0][n - 1], the range between the two pads.
The two dashed balloons are pads painted 1. They never burst; they only stand in for "nothing there", so the end balloons pay by the same rule as the rest. Each table cell will hold the most coins from bursting everything strictly between its two ends.
range0 to 2
last3index 1
total3
cell3
Between balloons 0 and 2, suppose the 3 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 3 × 1 = 3. Nothing lies between 0 and 1, so the left side adds 0. Nothing lies between 1 and 2, so the right side adds 0. This is the first choice tried here, so 3 is the best so far.
range1 to 3
last1index 2
total15
cell15
Between balloons 1 and 3, suppose the 1 at index 2 bursts last. By then everything else in the range is gone, so it pays 3 × 1 × 5 = 15. Nothing lies between 1 and 2, so the left side adds 0. Nothing lies between 2 and 3, so the right side adds 0. This is the first choice tried here, so 15 is the best so far.
range2 to 4
last5index 3
total40
cell40
Between balloons 2 and 4, suppose the 5 at index 3 bursts last. By then everything else in the range is gone, so it pays 1 × 5 × 8 = 40. Nothing lies between 2 and 3, so the left side adds 0. Nothing lies between 3 and 4, so the right side adds 0. This is the first choice tried here, so 40 is the best so far.
range3 to 5
last8index 4
total40
cell40
Between balloons 3 and 5, suppose the 8 at index 4 bursts last. By then everything else in the range is gone, so it pays 5 × 8 × 1 = 40. Nothing lies between 3 and 4, so the left side adds 0. Nothing lies between 4 and 5, so the right side adds 0. This is the first choice tried here, so 40 is the best so far.
range0 to 3
last3index 1
total30
cell30
Between balloons 0 and 3, suppose the 3 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 3 × 5 = 15. Nothing lies between 0 and 1, so the left side adds 0. Its right side, 1 to 3, was already solved: 15. This is the first choice tried here, so 30 is the best so far.
range0 to 3
last1index 2
total8
cell30
Between balloons 0 and 3, suppose the 1 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 1 × 5 = 5. Its left side, 0 to 2, was already solved: 3. Nothing lies between 2 and 3, so the right side adds 0. 8 does not beat 30, so the cell keeps 30.
range1 to 4
last1index 2
total64
cell64
Between balloons 1 and 4, suppose the 1 at index 2 bursts last. By then everything else in the range is gone, so it pays 3 × 1 × 8 = 24. Nothing lies between 1 and 2, so the left side adds 0. Its right side, 2 to 4, was already solved: 40. This is the first choice tried here, so 64 is the best so far.
range1 to 4
last5index 3
total135
cell135
Between balloons 1 and 4, suppose the 5 at index 3 bursts last. By then everything else in the range is gone, so it pays 3 × 5 × 8 = 120. Its left side, 1 to 3, was already solved: 15. Nothing lies between 3 and 4, so the right side adds 0. 135 beats 64, so the cell takes the new total.
range2 to 5
last5index 3
total45
cell45
Between balloons 2 and 5, suppose the 5 at index 3 bursts last. By then everything else in the range is gone, so it pays 1 × 5 × 1 = 5. Nothing lies between 2 and 3, so the left side adds 0. Its right side, 3 to 5, was already solved: 40. This is the first choice tried here, so 45 is the best so far.
range2 to 5
last8index 4
total48
cell48
Between balloons 2 and 5, suppose the 8 at index 4 bursts last. By then everything else in the range is gone, so it pays 1 × 8 × 1 = 8. Its left side, 2 to 4, was already solved: 40. Nothing lies between 4 and 5, so the right side adds 0. 48 beats 45, so the cell takes the new total.
range0 to 4
last3index 1
total159
cell159
Between balloons 0 and 4, suppose the 3 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 3 × 8 = 24. Nothing lies between 0 and 1, so the left side adds 0. Its right side, 1 to 4, was already solved: 135. This is the first choice tried here, so 159 is the best so far.
range0 to 4
last1index 2
total51
cell159
Between balloons 0 and 4, suppose the 1 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 1 × 8 = 8. Its left side, 0 to 2, was already solved: 3. Its right side, 2 to 4, was already solved: 40. 51 does not beat 159, so the cell keeps 159.
range0 to 4
last5index 3
total70
cell159
Between balloons 0 and 4, suppose the 5 at index 3 bursts last. By then everything else in the range is gone, so it pays 1 × 5 × 8 = 40. Its left side, 0 to 3, was already solved: 30. Nothing lies between 3 and 4, so the right side adds 0. 70 does not beat 159, so the cell keeps 159.
range1 to 5
last1index 2
total51
cell51
Between balloons 1 and 5, suppose the 1 at index 2 bursts last. By then everything else in the range is gone, so it pays 3 × 1 × 1 = 3. Nothing lies between 1 and 2, so the left side adds 0. Its right side, 2 to 5, was already solved: 48. This is the first choice tried here, so 51 is the best so far.
range1 to 5
last5index 3
total70
cell70
Between balloons 1 and 5, suppose the 5 at index 3 bursts last. By then everything else in the range is gone, so it pays 3 × 5 × 1 = 15. Its left side, 1 to 3, was already solved: 15. Its right side, 3 to 5, was already solved: 40. 70 beats 51, so the cell takes the new total.
range1 to 5
last8index 4
total159
cell159
Between balloons 1 and 5, suppose the 8 at index 4 bursts last. By then everything else in the range is gone, so it pays 3 × 8 × 1 = 24. Its left side, 1 to 4, was already solved: 135. Nothing lies between 4 and 5, so the right side adds 0. 159 beats 70, so the cell takes the new total.
range0 to 5
last3index 1
total162
cell162
Between balloons 0 and 5, suppose the 3 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 3 × 1 = 3. Nothing lies between 0 and 1, so the left side adds 0. Its right side, 1 to 5, was already solved: 159. This is the first choice tried here, so 162 is the best so far.
range0 to 5
last1index 2
total52
cell162
Between balloons 0 and 5, suppose the 1 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 1 × 1 = 1. Its left side, 0 to 2, was already solved: 3. Its right side, 2 to 5, was already solved: 48. 52 does not beat 162, so the cell keeps 162.
range0 to 5
last5index 3
total75
cell162
Between balloons 0 and 5, suppose the 5 at index 3 bursts last. By then everything else in the range is gone, so it pays 1 × 5 × 1 = 5. Its left side, 0 to 3, was already solved: 30. Its right side, 3 to 5, was already solved: 40. 75 does not beat 162, so the cell keeps 162.
range0 to 5
last8index 4
total167
cell167
Between balloons 0 and 5, suppose the 8 at index 4 bursts last. By then everything else in the range is gone, so it pays 1 × 8 × 1 = 8. Its left side, 0 to 4, was already solved: 159. Nothing lies between 4 and 5, so the right side adds 0. 167 beats 162, so the cell takes the new total.
answer167
The cell from 0 to 5 covers every real balloon, with only the two pads left standing. 167 is the best total over every choice of last balloon, and so over every bursting order.
balloons2
ranges to fill3
The two dashed balloons are pads painted 1. They never burst; they only stand in for "nothing there", so the end balloons pay by the same rule as the rest. Each table cell will hold the most coins from bursting everything strictly between its two ends.
range0 to 2
last1index 1
total5
cell5
Between balloons 0 and 2, suppose the 1 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 1 × 5 = 5. Nothing lies between 0 and 1, so the left side adds 0. Nothing lies between 1 and 2, so the right side adds 0. This is the first choice tried here, so 5 is the best so far.
range1 to 3
last5index 2
total5
cell5
Between balloons 1 and 3, suppose the 5 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 5 × 1 = 5. Nothing lies between 1 and 2, so the left side adds 0. Nothing lies between 2 and 3, so the right side adds 0. This is the first choice tried here, so 5 is the best so far.
range0 to 3
last1index 1
total6
cell6
Between balloons 0 and 3, suppose the 1 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 1 × 1 = 1. Nothing lies between 0 and 1, so the left side adds 0. Its right side, 1 to 3, was already solved: 5. This is the first choice tried here, so 6 is the best so far.
range0 to 3
last5index 2
total10
cell10
Between balloons 0 and 3, suppose the 5 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 5 × 1 = 5. Its left side, 0 to 2, was already solved: 5. Nothing lies between 2 and 3, so the right side adds 0. 10 beats 6, so the cell takes the new total.
answer10
The cell from 0 to 3 covers every real balloon, with only the two pads left standing. 10 is the best total over every choice of last balloon, and so over every bursting order.
balloons3
ranges to fill6
The two dashed balloons are pads painted 1. They never burst; they only stand in for "nothing there", so the end balloons pay by the same rule as the rest. Each table cell will hold the most coins from bursting everything strictly between its two ends.
range0 to 2
last2index 1
total0
cell0
Between balloons 0 and 2, suppose the 2 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 2 × 0 = 0. Nothing lies between 0 and 1, so the left side adds 0. Nothing lies between 1 and 2, so the right side adds 0. This is the first choice tried here, so 0 is the best so far.
range1 to 3
last0index 2
total0
cell0
Between balloons 1 and 3, suppose the 0 at index 2 bursts last. By then everything else in the range is gone, so it pays 2 × 0 × 3 = 0. Nothing lies between 1 and 2, so the left side adds 0. Nothing lies between 2 and 3, so the right side adds 0. This is the first choice tried here, so 0 is the best so far.
range2 to 4
last3index 3
total0
cell0
Between balloons 2 and 4, suppose the 3 at index 3 bursts last. By then everything else in the range is gone, so it pays 0 × 3 × 1 = 0. Nothing lies between 2 and 3, so the left side adds 0. Nothing lies between 3 and 4, so the right side adds 0. This is the first choice tried here, so 0 is the best so far.
range0 to 3
last2index 1
total6
cell6
Between balloons 0 and 3, suppose the 2 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 2 × 3 = 6. Nothing lies between 0 and 1, so the left side adds 0. Its right side, 1 to 3, was already solved: 0. This is the first choice tried here, so 6 is the best so far.
range0 to 3
last0index 2
total0
cell6
Between balloons 0 and 3, suppose the 0 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 0 × 3 = 0. Its left side, 0 to 2, was already solved: 0. Nothing lies between 2 and 3, so the right side adds 0. 0 does not beat 6, so the cell keeps 6.
range1 to 4
last0index 2
total0
cell0
Between balloons 1 and 4, suppose the 0 at index 2 bursts last. By then everything else in the range is gone, so it pays 2 × 0 × 1 = 0. Nothing lies between 1 and 2, so the left side adds 0. Its right side, 2 to 4, was already solved: 0. This is the first choice tried here, so 0 is the best so far.
range1 to 4
last3index 3
total6
cell6
Between balloons 1 and 4, suppose the 3 at index 3 bursts last. By then everything else in the range is gone, so it pays 2 × 3 × 1 = 6. Its left side, 1 to 3, was already solved: 0. Nothing lies between 3 and 4, so the right side adds 0. 6 beats 0, so the cell takes the new total.
range0 to 4
last2index 1
total8
cell8
Between balloons 0 and 4, suppose the 2 at index 1 bursts last. By then everything else in the range is gone, so it pays 1 × 2 × 1 = 2. Nothing lies between 0 and 1, so the left side adds 0. Its right side, 1 to 4, was already solved: 6. This is the first choice tried here, so 8 is the best so far.
range0 to 4
last0index 2
total0
cell8
Between balloons 0 and 4, suppose the 0 at index 2 bursts last. By then everything else in the range is gone, so it pays 1 × 0 × 1 = 0. Its left side, 0 to 2, was already solved: 0. Its right side, 2 to 4, was already solved: 0. 0 does not beat 8, so the cell keeps 8.
range0 to 4
last3index 3
total9
cell9
Between balloons 0 and 4, suppose the 3 at index 3 bursts last. By then everything else in the range is gone, so it pays 1 × 3 × 1 = 3. Its left side, 0 to 3, was already solved: 6. Nothing lies between 3 and 4, so the right side adds 0. 9 beats 8, so the cell takes the new total.
answer9
The cell from 0 to 4 covers every real balloon, with only the two pads left standing. 9 is the best total over every choice of last balloon, and so over every bursting order.
05
Recursion with memo
best(left, right) returns the most coins from the balloons strictly between left and right. It tries every m as the last balloon and adds the two smaller ranges, which the cache answers after their first call. An empty range returns 0 because the loop does not run.
▶1from functools import cache
▶2
▶3
▶4class Solution:
▶5defmaxCoins(self, nums):
▶6 vals = [1] + nums + [1]
▶7
▶8 @cache
▶9defbest(left, right):
▶10 result = 0
▶11for m inrange(left + 1, right):
▶12 coins = vals[left] * vals[m] * vals[right]
▶13 result = max(result, coins + best(left, m) + best(m, right))
▶25 result = Math.max(result, coins + best(left, m) + best(m, right));
▶26 }
▶27return memo[left][right] = result;
▶28 }
▶29}
06
Common pitfalls
Paying with the balloon's original neighbours
✗ Wrong
coins = vals[m - 1] * vals[m] * vals[m + 1]
✓ Right
coins = vals[left] * vals[m] * vals[right]
When m bursts last in its range, the balloons at m - 1 and m + 1 may be long gone; the ones next to it are left and right. On [3, 1, 5, 8] the wrong line returns 98 instead of 167.
Leaving out the pads
✗ Wrong
vals = nums
✓ Right
vals = [1] + nums + [1]
Without pads, the first and last balloons become the range boundaries, so they are never burst and never pay. On [3, 1, 5, 8] this returns 135 instead of 167.
Filling the table row by row
✗ Wrong
for left in range(n):
for right in range(left + 2, n):
✓ Right
for gap in range(2, n):
for left in range(n - gap):
The total for (left, right) reads dp[m][right] with m > left, a row the loop has not reached yet, so it reads 0. On [3, 1, 5, 8] this returns 63. Going by gap, or with left counting down, fills every smaller range first.
07
Edge cases
One balloon
The only range is the one between the two pads, so the answer is 1 * nums[0] * 1, which is just nums[0].
Balloons painted 0
No special code is needed. Any burst next to a 0 pays 0, so the best order tends to burst the 0 early, and the table finds that because it tries every last balloon.
08
Complexity
Time
O(n³)
Space
O(n²)
There are about n²/2 ranges and each tries up to n last balloons, roughly n³/6 ≈ 4.5 million products when n = 300. The table has (n + 2)² cells. The largest total, 300 × 100³ = 3·10⁸, fits in a 32-bit int.
09
Burst Balloons FAQ
How do I get the actual bursting order, not just the total?
Store the best m for every range. The range between the two pads bursts its m last; before that, each side bursts in its own stored order. For [3, 1, 5, 8] that gives 1, 5, 3, 8: it pays 15, then 120, then 24, then 8, for 167.
Is this the same idea as Matrix Chain Multiplication?
The shape is the same: pick a split point inside a range, pay a cost that depends on the two ends and the split, and add the best of each side. In Matrix Chain Multiplication the split is the last multiplication and you minimise; here it is the last balloon and you maximise.