Burst Balloons
Question (LC.312)
Given n balloons b_0, b_1, ..., b_{n-1}, each balloon contains a number of coins. The player needs to shoot all balloons and maximize her score. The score from each balloon will be coins[i-1] * coins[i] * coins[i+1]. After b_i is getting shot, b_{i-1} and b_{i+1} bocome adjacent to each other.
Example
I: []
O: 0
I: [3,1,5]
O: shoot 1 (3*1*5) + shoot 3 (3*5) + shoot 5 (5) = 15 + 15 + 5 = 35
I: [5,4,3,2,10]
O: 440Brute Force Search
We can do a brute force search by trying every possible bursts and return a maximum. Then, we observe the fact that there are overlapping subproblems. We can add in a cache mechanism to speed it up.

But we can't quite find a way to do a full DP because we can't define a subproblem that can be represented by indices.
Cached Brute Force
The state representation is very inefficient here (adding and removing coins from the list are very costly operations). A simple test case [8,2,6,8,9,8,1,4,1,5,3,0,7,7,0,4,2,2,5,5] will exceed the time limit on LC.
Note: Using mutable objects as hashing key can be very dangerous. When the input size gets large enough, there will be collisions and wrong returning values.
Redefine Subproblem
The way we defined the state previous is by thinking on the first balloon that we want to burst. Let's try another way to define the state in a bottom-up fashion. What is the last balloon that we want to burst?

Cached Interval Search
Bottom Up Interval DP
This is an optimization problem with a single sequence. Since each decision depends on left and right, the state needs to be defined by substrings or intervals. Prefix or suffix will not be a suffient representation when breaking down the current problem into subproblems.
DP Code
Time & Space Complexity
Last updated