Burst Balloons
Find the popping order that earns the most points
DSA, Hard, 2-D DP
Which structure or algorithm, and what does it cost.
A row of balloons hangs on a string. The list values gives the score printed on each balloon. You pop the balloons one at a time. Popping a balloon earns its score times the score of the balloon now on its left times the score of the balloon now on its right. A side with no balloon counts as 1. Pop every balloon in the best order and return the largest total.
Examples
Constraints
- 0 <= length of
values<= 80. - 0 <= each score <= 60.
Your answer must contain
- The simple method and why it is too slow; the better method; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n^3) time and O(n^2) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Popping a balloon changes the neighbours of the balloons beside it. Within a stretch of balloons, which one keeps fixed neighbours for the longest?
Hint 2 the approach
Think of the last balloon popped in a stretch. When it goes, its neighbours are the two ends of the stretch, and the parts on each side are independent.
Hint 3 the key step
Try each balloon as the last one popped between two fixed edges. What does it earn, and which two smaller problems are left?
Walkthrough
Full walkthrough brute force to best
Brute force
Try every popping order and score each one. There are n! orders. For 80 balloons that is more than 10^100, so this only works for about 10 balloons.
Better method
Recursion with memory. Let best(l, r) be the top score from the balloons strictly between edge l and edge r. Try each balloon k between them as the last one popped: best(l, k) + v[l] * v[k] * v[r] + best(k, r). Store each pair. There are about n^2 / 2 pairs and each tries up to n balloons, so O(n^3) time and O(n^2) memory.
Best method
Pad the list with a 1 at each end. Let best[l][r] be the top score from the balloons strictly between positions l and r, which are treated as fixed edges. Think of the last balloon popped between the edges, call it k. When it goes, only l and r are its neighbours, so it earns v[l] * v[k] * v[r]. The balloons between l and k, and between k and r, were popped earlier and never touch each other, because k was still standing between them. So their scores are best[l][k] and best[k][r]. Take the largest total over every k. Fill the table by gap r - l, from 2 upward, so the smaller parts are ready. The answer is best[0][n - 1] for the padded length n. No balloons gives 0.
Hand trace
Run on values = [2, 5, 3]. The padded list is [1, 2, 5, 3, 1]. Gap 2: best[0][2] = 10, best[1][3] = 30, best[2][4] = 15. Gap 3: best[0][3] = 36, best[1][4] = 36. Gap 4: best[0][4] = 39. The answer is best[0][4], 39.
Code
def max_burst_score(values):
v = [1] + list(values) + [1]
n = len(v)
best = [[0] * n for _ in range(n)] # best[l][r]: top score from the balloons strictly between l and r
for gap in range(2, n):
for l in range(n - gap):
r = l + gap
best[l][r] = max(best[l][k] + v[l] * v[k] * v[r] + best[k][r] for k in range(l + 1, r))
return best[0][n - 1]Time and memory
O(n^3) time: about n^2 / 2 pairs of edges, and each tries up to n balloons. O(n^2) memory for the table.
Common mistakes
- Choosing the first balloon to pop. Its neighbours change after each pop, so the left and right parts depend on each other. Choose the last one.
- Forgetting the padding. A balloon at the end has no neighbour on one side, and that side must count as 1.
- Filling the table in the wrong order. Cell (l, r) reads cells with a smaller gap, so the gaps must go from small to large.