Burst Balloons
Master this topic with zero to advance depth.
Burst Balloons
You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it, represented by an array nums. You are asked to burst all the balloons.
If you burst the i-th balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. After bursting it, the i - 1-th and i + 1-th balloons become adjacent.
Find the maximum coins you can collect by bursting the balloons wisely. (Assume nums[-1] = nums[n] = 1).
Visual Representation
nums = [3, 1, 5, 8]
Add boundary: [1, 3, 1, 5, 8, 1]
If we burst 1 first:
Coins = 3 * 1 * 5 = 15
Remaining: [1, 3, 5, 8, 1]
Wait! It's better to think: "Which balloon is burst LAST in range (i, j)?"Examples
nums = [3,1,5,8] -> [3,5,8] -> [3,8] -> [8] -> [] coins = 315 + 358 + 138 + 181 = 15 + 120 + 24 + 8 = 167
Level I: Brute Force (Recursion)
Intuition
Pick a balloon k to burst last in the interval (i, j). This splits the problem into two subproblems: bursting balloons in (i, k) and (k, j). Try all possible candidates for the last balloon.
Thought Process
- Pad the array with 1 at both ends.
- Define a function
solve(i, j)that returns max coins from interval[i, j]. - Base case: If
i > j, return 0. - Recursive step:
max(solve(i, k-1) + solve(k+1, j) + A[i-1]*A[k]*A[j+1])for allkin[i, j].
Detailed Dry Run
nums=[3, 1, 5] -> A=[1, 3, 1, 5, 1] solve(1, 3) where length is 3. Try k=2 as last: solve(1, 1) + solve(3, 3) + 111 = 3 + 25 + 1 = 29.
Level II: Memoization (Top-Down Interval DP)
Intuition
The brute force re-solves the same (i, j) intervals. We add a 2D memo table to cache the max coins for each interval.
Visual
nums=[3,1,5,8], A=[1,3,1,5,8,1]
memo[1][4] = max coins for range (1..4)
After first computation, reused whenever (1,4) is subproblem!Detailed Dry Run
A=[1,3,1,5,8,1]. memo[1][4]: try k=1,2,3,4 as last balloon. k=3 gives 351+memo[1][2]+memo[4][4]=15+9+40=... Cached!
⚠️ Common Pitfalls & Tips
This is an interval DP problem. The key insight — picking the LAST balloon to burst — allows clean decomposition. Memoization here gives the same O(N^3) as bottom-up but is easier to reason about.
Level III: Dynamic Programming (Interval/Matrix Chain)
Intuition
Instead of deciding which balloon to burst first, we decide which balloon k is burst last in the interval (i, j). This allows us to split the problem into two independent subproblems: bursting balloons in (i, k) and (k, j).
Thought Process
- Pad the array with
1at both ends. dp[i][j]= max coins from bursting balloons between indexiandj(exclusive).- For each length
lenfrom 1 ton:- For each start
ifrom 1 ton - len + 1:- End
j = i + len - 1. - For
kfromitoj(last balloon to burst):coins = nums[i-1] * nums[k] * nums[j+1] + dp[i][k-1] + dp[k+1][j].
- End
- For each start
Detailed Dry Run
nums = [3, 1, 5] Boundary padded: [1, 3, 1, 5, 1]
| Interval | Last k | Calculation | Result |
|---|---|---|---|
| (1,1) | 1 | 131 + 0 + 0 | 3 |
| (2,2) | 2 | 315 + 0 + 0 | 15 |
| (3,3) | 3 | 551 + 0 + 0 | 25 |
| (1,2) | 1 | 135 + 0 + dp[2][2] = 15+15 | 30 |
| (1,2) | 2 | 115 + dp[1][1] + 0 = 5+3 | 8 |
⚠️ Common Pitfalls & Tips
The intuition for O(N^3) interval DP is often hard to find. Remember: "last item to be processed" is a common pattern for problems involving shrinking ranges (like Matrix Chain Multiplication).
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.