Sub topic 13
Thinking Backwards: The Last Operation in an Interval
This chapter assumes you already know the split-point recurrence, filling a table by interval length, and top-down memoization from earlier chapters. Here we take on problems where splitting on the first move fails, and fix them by splitting on the last move.
Why the first move breaks independence
Burst balloons is the standard example. You get a row of balloons with values nums. Bursting balloon k earns left * nums[k] * right, where left and right are the values of its current neighbors. A side with no neighbor counts as 1. Once a balloon bursts, its neighbors become adjacent. The goal is to maximize the total coins.
The natural first attempt picks the first balloon to burst, k, and recurses on the two sides. That doesn't work. After k is gone, the rightmost balloon of the left side now borders the leftmost balloon of the right side. The coins earned on the left then depend on what happens on the right. The two subproblems share a boundary that keeps moving, so they are not independent, and an interval DP needs independent subproblems.
Choose the balloon that bursts last
Now pick k as the last balloon to burst inside an interval. While every other balloon in the interval is being burst, k stays put and acts as a fixed wall. Balloons to its left can never become adjacent to balloons on its right. When k finally bursts, its neighbors are the two interval boundaries, which are also fixed.
That gives a clean state with exclusive bounds:
best(left, right)is the maximum number of coins from bursting every balloon strictly betweenleftandright. The balloons atleftandrightthemselves are still standing.- Pad the array with a 1 at each end so the outer boundaries always exist.
- Base case: if
right == left + 1, the open interval is empty and the answer is 0.
The recurrence is:
best(left, right) = max over left < k < right of best(left, k) + vals[left] * vals[k] * vals[right] + best(k, right)
Implementation
from functools import lru_cache
def max_coins(nums):
vals = [1] + nums + [1]
@lru_cache(maxsize=None)
def best(left, right):
# Best total from bursting every balloon strictly between left and right.
result = 0
for k in range(left + 1, right):
gain = vals[left] * vals[k] * vals[right]
result = max(result, best(left, k) + gain + best(k, right))
return result
return best(0, len(vals) - 1)
print(max_coins([3, 1, 5, 8])) # 167The empty-interval base case needs no special code. When right == left + 1, the loop body never runs and the function returns 0. There are O(n²) states, each takes O(n) work, so the total is O(n³) time and O(n²) memory. These are the same bounds as other split-point DPs.
Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Inclusive bounds `[i, j]` with neighbors taken as `nums[i-1]`, `nums[j+1]` | Off-by-one reads, wrong totals at the edges | Use exclusive bounds over a padded array |
| Forgetting the sentinel 1s | Index errors, or edge balloons scored with 0 | `vals = [1] + nums + [1]` |
| Recursing on `(left, k - 1)` and `(k + 1, right)` | Boundary balloon `k` is lost as a wall | Pass `k` itself as the shared boundary |
| Thinking "first burst" and "last burst" are symmetric | Recurrence looks right but gives wrong answers | Only the last operation fixes both neighbors |
Exercises
Exercise
Trace max_coins([1, 5]) by hand. Which balloon should burst last, and what is the total?
Show answer
vals = [1, 1, 5, 1]. If the 1 bursts last: the 5 first earns 151 = 5, then the 1 earns 111 = 1, for a total of 6. If the 5 bursts last: the 1 first earns 115 = 5, then the 5 earns 151 = 5, for a total of 10. The answer is 10, with the 5 burst last.
Exercise
You want a bottom-up version of best(left, right). In what order must the table be filled, and why does the gap right - left matter?
Show answer
Fill by increasing gap = right - left, starting at gap 2 (gap 1 is the empty base case, value 0). Each state reads best(left, k) and best(k, right), and both have a strictly smaller gap than best(left, right), so they are already computed.