Burst Balloons (LeetCode 312): Solution and Explanation
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.
Watch it run step by step
Try your own input and write the code →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
- Pad the input:
nums = [1] + nums + [1], and letnbe the new length. - Create an n by n table
dpof zeros. Windows with no balloons inside (width 1) stay 0. - Loop
lengthfrom 2 to n - 1 andleftfrom 0 whileright = left + lengthstays in range. - For each
kstrictly betweenleftandright, computedp[left][k] + nums[left] * nums[k] * nums[right] + dp[k][right]. - Keep the maximum of those values in
dp[left][right]. - Return
dp[0][n-1].
Step-by-step example
nums = [3, 1, 5, 8]
- Padded array is
[1, 3, 1, 5, 8, 1], indices 0 to 5. - 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. - 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. - 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. - 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]1class Solution {
2 public int maxCoins(int[] nums) {
3 int[] arr = new int[nums.length + 2];
4 arr[0] = arr[nums.length + 1] = 1;
5 for (int i = 0; i < nums.length; i++) arr[i + 1] = nums[i];
6 int n = arr.length;
7 int[][] dp = new int[n][n];
8
9 for (int length = 2; length < n; length++) {
10 for (int left = 0; left < n - length; left++) {
11 int right = left + length;
12 for (int k = left + 1; k < right; k++) {
13 int coins = arr[left] * arr[k] * arr[right];
14 dp[left][right] = Math.max(dp[left][right],
15 dp[left][k] + coins + dp[k][right]);
16 }
17 }
18 }
19
20 return dp[0][n - 1];
21 }
22}1class Solution {
2public:
3 int maxCoins(vector<int>& nums) {
4 vector<int> arr = {1};
5 for (int x : nums) arr.push_back(x);
6 arr.push_back(1);
7 int n = arr.size();
8 vector<vector<int>> dp(n, vector<int>(n, 0));
9
10 for (int length = 2; length < n; length++) {
11 for (int left = 0; left < n - length; left++) {
12 int right = left + length;
13 for (int k = left + 1; k < right; k++) {
14 int coins = arr[left] * arr[k] * arr[right];
15 dp[left][right] = max(dp[left][right],
16 dp[left][k] + coins + dp[k][right]);
17 }
18 }
19 }
20
21 return dp[0][n - 1];
22 }
23};Time and space complexity
O(n³)
There are O(n²) windows and each tries O(n) choices for the last balloon.
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
koverleftandrightthemselves. The boundaries are never burst inside their own window, sokmust be strictly between them. - Filling the table by
leftfrom 0 upward withrightgrowing. The values for narrower windows would not be ready yet; iterate by width or withleftdescending.
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.