Burst Balloons
You have n balloons in a row, and balloon i has the number nums[i] painted on it. When you burst balloon i, you earn left * nums[i] * right coins, where left and right are the numbers on the balloons currently next to it. Once a balloon bursts, its two neighbors become next to each other.
If there's no balloon on one side, treat that side as a balloon with the number 1. Burst every balloon, in whatever order you like, and return the most coins you can earn.
Example 1
Input: nums = [3, 1, 5, 8] Output: 167
One best order: burst 1 for 3 * 1 * 5 = 15, then 5 for 3 * 5 * 8 = 120, then 3 for 1 * 3 * 8 = 24, then 8 for 1 * 8 * 1 = 8. The total is 167.
Example 2
Input: nums = [1, 5] Output: 10
Bursting 1 first earns 1 * 1 * 5 = 5, then 5 earns 1 * 5 * 1 = 5.
Constraints
1 <= nums.length <= 3000 <= nums[i] <= 100
Approach
Trying every order costs O(n!). The natural recursion, picking the first balloon to burst, doesn't help: after it bursts, its old neighbors touch, so the left and right parts still interact.
Pick the last balloon instead. Pad the row with a 1 on each end, which stand for the empty sides and are never burst. Now consider every balloon strictly between positions left and right, with those two still standing. If balloon last bursts last, everything else in the range has already burst, so its neighbors at that moment are left and right and it earns vals[left] * vals[last] * vals[right]. Before that, the balloons between left and last and the balloons between last and right burst independently, since last separates them the whole time. That gives the recurrence:
dp[left][right] = max over last of vals[left] * vals[last] * vals[right] + dp[left][last] + dp[last][right]
Fill the table by increasing range length, so the smaller ranges are ready when a larger one needs them. The answer is dp[0][n + 1], the range covering every real balloon.
Steps
- Build
vals = [1] + nums + [1]. - Create a table
dpof zeros. Ranges with no balloon inside score0. - For each range length from
2upward, and eachleft, setright = left + length. - For each
laststrictly between them, compute the score withlastburst last and keep the maximum indp[left][right]. - Return
dp[0][len(vals) - 1].
from typing import List
def max_coins(nums: List[int]) -> int:
vals = [1] + nums + [1] # the virtual 1s at both ends are never burst
n = len(vals)
# dp[left][right] = best coins from bursting every balloon strictly between left and right
dp = [[0] * n for _ in range(n)]
for length in range(2, n): # distance between the two walls
for left in range(n - length):
right = left + length
for last in range(left + 1, right): # the balloon burst last in this range
coins = vals[left] * vals[last] * vals[right] + dp[left][last] + dp[last][right]
dp[left][right] = max(dp[left][right], coins)
return dp[0][n - 1]Complexity
- Time:
O(n^3). There areO(n^2)ranges, and each triesO(n)choices for the last balloon. - Space:
O(n^2)for the table.
Common mistakes
- Choosing the first balloon to burst in the recursion, which leaves the two sides dependent on each other.
- Using the original neighbors
nums[last - 1]andnums[last + 1]in the formula instead of the range wallsvals[left]andvals[right]. - Filling the table row by row instead of by range length, so a larger range reads smaller ranges that haven't been computed yet.