Burst Balloons (LeetCode 312): Solution and Explanation

Hard
Array
Dynamic Programming

You have a row of balloons with numbers on them. Bursting one earns the product of its number and its current left and right neighbors, treating anything past the ends as 1, and the neighbors then become adjacent. Find the most coins you can collect by bursting all of them.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

Trying every order is n factorial, so we need structure. The tempting move is to choose which balloon to burst first in a range and recurse on the two sides. That fails, because after the first burst the left and right sides are still adjacent and their future coins depend on each other.

The trick is to choose which balloon bursts last in a range instead. Fix a window with fixed boundaries left and right that are not burst inside this subproblem. If balloon k is the last one popped between them, then at the moment it bursts its neighbors are exactly left and right, so it earns nums[left] * nums[k] * nums[right]. Before that, everything between left and k was popped without ever seeing anything right of k, and the same holds on the other side. The two halves are now independent.

Padding the array with a 1 on each end gives every balloon real boundaries. Then dp[left][right] is the best score for bursting everything strictly between the two indices, and the answer is dp[0][n-1] over the padded array. Windows are filled in order of increasing width so the smaller pieces are ready first.

Approach: Interval DP choosing the last balloon

  1. Pad the input: nums = [1] + nums + [1], and let n be the new length.
  2. Create an n by n table dp of zeros. Windows with no balloons inside (width 1) stay 0.
  3. Loop length from 2 to n - 1 and left from 0 while right = left + length stays in range.
  4. For each k strictly between left and right, compute dp[left][k] + nums[left] * nums[k] * nums[right] + dp[k][right].
  5. Keep the maximum of those values in dp[left][right].
  6. Return dp[0][n-1].

Step-by-step example

nums = [3, 1, 5, 8]
  1. Padded array is [1, 3, 1, 5, 8, 1], indices 0 to 5.
  2. Width 2 windows each hold one balloon: dp[0][2] = 1 × 3 × 1 = 3, dp[1][3] = 3 × 1 × 5 = 15, dp[2][4] = 1 × 5 × 8 = 40, dp[3][5] = 5 × 8 × 1 = 40.
  3. Window (1, 4) holds balloons 1 and 5 between 3 and 8. Making 5 last gives dp[1][3] + 3 × 5 × 8 + 0 = 15 + 120 = 135, the best option.
  4. Window (0, 4) holds 3, 1, 5 between 1 and 8. Making 3 last gives 0 + 1 × 3 × 8 + dp[1][4] = 24 + 135 = 159.
  5. Window (0, 5) is the whole row. Making 8 last gives dp[0][4] + 1 × 8 × 1 = 159 + 8 = 167, which is the answer.

Code: Python, Java, C++

1class Solution:
2    def maxCoins(self, nums: List[int]) -> int:
3        nums = [1] + nums + [1]
4        n = len(nums)
5        dp = [[0] * n for _ in range(n)]
6
7        for length in range(2, n):
8            for left in range(n - length):
9                right = left + length
10                for k in range(left + 1, right):
11                    coins = nums[left] * nums[k] * nums[right]
12                    dp[left][right] = max(dp[left][right],
13                        dp[left][k] + coins + dp[k][right])
14
15        return dp[0][n - 1]

Time and space complexity

Time

O(n³)

There are O(n²) windows and each tries O(n) choices for the last balloon.

Space

O(n²)

The DP table has one entry per pair of boundaries in the padded array.

Other approaches

Top-down memoized recursion (time O(n³), space O(n²))

Define solve(left, right) with the same last-balloon recurrence and cache it. It is often easier to derive and visits only reachable windows, with the same bounds.

Brute force over orders (time O(n!), space O(n))

Try every bursting order with backtracking. It is only useful to check answers on tiny inputs.

Common mistakes

  • Choosing the first balloon to burst in a window. The sides then still interact, so the subproblems are not independent and the recurrence is wrong.
  • Forgetting to pad with 1s, which makes the boundary products special cases and usually leads to index errors.
  • Iterating k over left and right themselves. The boundaries are never burst inside their own window, so k must be strictly between them.
  • Filling the table by left from 0 upward with right growing. The values for narrower windows would not be ready yet; iterate by width or with left descending.

Frequently asked questions

Why do we pick the last balloon instead of the first?

If k is the last balloon burst in a window, its neighbors at that moment are the fixed boundaries, and the two sides never touch each other. That independence is what makes the subproblems valid.

Why pad the array with 1 on both ends?

Bursting at the edge treats the missing neighbor as 1. Adding real 1s makes every balloon have boundaries and removes all special cases from the formula.

What is the time complexity of Burst Balloons?

O(n³): about n² windows times up to n choices of the last balloon. Space is O(n²) for the table.

Is Burst Balloons similar to matrix chain multiplication?

Yes. Both are interval DPs where you pick a split point inside a range and combine two independent subranges with a cost based on the boundaries.