Problem 59 · Dynamic Programming
Burst Balloons
01Problem
Bursting balloon i earns nums[left]*nums[i]*nums[right] where left/right are current neighbors (out of bounds = 1). Return the maximum coins from bursting all balloons.
02Key insight
Think about the LAST balloon burst in an interval: its neighbors are the fixed interval boundaries, which makes subproblems independent. Interval DP over padded array.
03Solution
Try it first.
TimeO(n^3)
SpaceO(n^2)
tests.py · 4 passing
tests.pypython4 lines
assert max_coins([3,1,5,8]) == 167
assert max_coins([1,5]) == 10
assert max_coins([]) == 0
assert max_coins([0, 2]) == 204Staff-level follow-up
Recognising 'choose last, not first' generalises: matrix-chain multiplication and optimal query join ordering in Snowflake/Spark optimizers use the same interval DP.