LeetCode #312 Hard

Burst Balloons

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.
Constraints
  • n == nums.length
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100
arrayinterval-dpdynamic-programming
Open on LeetCode ↗
02

Intuition

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.

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxCoins(self, nums):
▶3 vals = [1] + nums + [1]
▶4 n = len(vals)
▶5 dp = [[0] * n for _ in range(n)]
▶6 for gap in range(2, n):
▶7 for left in range(n - gap):
▶8 right = left + gap
▶9 for m in range(left + 1, right):
▶10 coins = vals[left] * vals[m] * vals[right]
▶11 coins += dp[left][m] + dp[m][right]
▶12 if coins > dp[left][right]:
▶13 dp[left][right] = coins
▶14 return dp[0][n - 1]
row with pads103112538415no range tried yetbest total for each rangeto 2to 3to 4to 5from 0from 1from 2from 3put a 1 at each end of the row
balloons4
ranges to fill10
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.
row with pads10left31last12right5384150+3+0=3left side1 × 3 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 03from 1from 2from 3from 0 to 2, 3 bursts last: 3, first try
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.
row with pads1031left12last53right84150+15+0=15left side3 × 1 × 5right sidenew bestbest total for each rangeto 2to 3to 4to 5from 03from 115from 2from 3from 1 to 3, 1 bursts last: 15, first try
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.
row with pads103112left53last84right150+40+0=40left side1 × 5 × 8right sidenew bestbest total for each rangeto 2to 3to 4to 5from 03from 115from 240from 3from 2 to 4, 5 bursts last: 40, first try
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.
row with pads10311253left84last15right0+40+0=40left side5 × 8 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 03from 115from 240from 340from 3 to 5, 8 bursts last: 40, first try
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.
row with pads10left31last1253right84150+15+15=30left side1 × 3 × 5right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330from 115from 240from 340from 0 to 3, 3 bursts last: 30, first try
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.
row with pads10left3112last53right84153+5+0=8left side1 × 1 × 5right sidenot betterbest total for each rangeto 2to 3to 4to 5from 0330from 115from 240from 340from 0 to 3, 1 bursts last: 8, keep 30
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.
row with pads1031left12last5384right150+24+40=64left side3 × 1 × 8right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330from 11564from 240from 340from 1 to 4, 1 bursts last: 64, first try
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.
row with pads1031left1253last84right1515+120+0=135left side3 × 5 × 8right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330from 115135from 240from 340from 1 to 4, 5 bursts last: 135 beats 64
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.
row with pads103112left53last8415right0+5+40=45left side1 × 5 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330from 115135from 24045from 340from 2 to 5, 5 bursts last: 45, first try
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.
row with pads103112left5384last15right40+8+0=48left side1 × 8 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330from 115135from 24048from 340from 2 to 5, 8 bursts last: 48 beats 45
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.
row with pads10left31last125384right150+24+135=159left side1 × 3 × 8right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330159from 115135from 24048from 340from 0 to 4, 3 bursts last: 159, first try
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.
row with pads10left3112last5384right153+8+40=51left side1 × 1 × 8right sidenot betterbest total for each rangeto 2to 3to 4to 5from 0330159from 115135from 24048from 340from 0 to 4, 1 bursts last: 51, keep 159
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.
row with pads10left311253last84right1530+40+0=70left side1 × 5 × 8right sidenot betterbest total for each rangeto 2to 3to 4to 5from 0330159from 115135from 24048from 340from 0 to 4, 5 bursts last: 70, keep 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.
row with pads1031left12last538415right0+3+48=51left side3 × 1 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330159from 11513551from 24048from 340from 1 to 5, 1 bursts last: 51, first try
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.
row with pads1031left1253last8415right15+15+40=70left side3 × 5 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330159from 11513570from 24048from 340from 1 to 5, 5 bursts last: 70 beats 51
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.
row with pads1031left125384last15right135+24+0=159left side3 × 8 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330159from 115135159from 24048from 340from 1 to 5, 8 bursts last: 159 beats 70
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.
row with pads10left31last12538415right0+3+159=162left side1 × 3 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330159162from 115135159from 24048from 340from 0 to 5, 3 bursts last: 162, first try
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.
row with pads10left3112last538415right3+1+48=52left side1 × 1 × 1right sidenot betterbest total for each rangeto 2to 3to 4to 5from 0330159162from 115135159from 24048from 340from 0 to 5, 1 bursts last: 52, keep 162
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.
row with pads10left311253last8415right30+5+40=75left side1 × 5 × 1right sidenot betterbest total for each rangeto 2to 3to 4to 5from 0330159162from 115135159from 24048from 340from 0 to 5, 5 bursts last: 75, keep 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.
row with pads10left31125384last15right159+8+0=167left side1 × 8 × 1right sidenew bestbest total for each rangeto 2to 3to 4to 5from 0330159167from 115135159from 24048from 340from 0 to 5, 8 bursts last: 167 beats 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.
row with pads103112538415the answer is the range between the two padsbest total for each rangeto 2to 3to 4to 5from 0330159167from 115135159from 24048from 340answer: 167 coins
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.
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:
▶5 def maxCoins(self, nums):
▶6 vals = [1] + nums + [1]
▶7 
▶8 @cache
▶9 def best(left, right):
▶10 result = 0
▶11 for m in range(left + 1, right):
▶12 coins = vals[left] * vals[m] * vals[right]
▶13 result = max(result, coins + best(left, m) + best(m, right))
▶14 return result
▶15 
▶16 return best(0, len(vals) - 1)
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.