Sub topic 3
Counting States and Transitions: O(n^2) Space, O(n^3) Time
This chapter assumes you can write a split-point recurrence, fill a table by increasing interval length, and read Big-O bounds.
Counting States and Transitions
An interval DP has one state for each pair i <= j. That is about n²/2 states, so the table needs O(n²) space. A split-point recurrence tries every k in [i, j), which costs O(n) work per state and O(n³) time in total. An endpoint-shrinking recurrence reads a constant number of neighbors, so it runs in O(n²) time.
| Recurrence shape | States | Work per state | Time |
|---|---|---|---|
| Split point | O(n²) | O(n) | O(n³) |
| Endpoint shrinking | O(n²) | O(1) | O(n²) |
Top-Down Interval DP with Memoization
You can also write the recurrence as a recursive function and cache it with functools.lru_cache. The cache handles evaluation order for you. The trade-off is recursion depth, which grows to O(n), and the overhead of each function call.
Constant-Time Interval Costs with Prefix Sums
Many recurrences add the cost of the whole interval, such as sum(a[i..j]). If you recompute that sum inside the loop, you add another factor of n. Precompute prefix[t] = a[0] + ... + a[t-1] once. Then each interval sum is prefix[j+1] - prefix[i], which takes O(1).
Worked Example: Merging Adjacent Piles
Merging two adjacent piles costs their combined weight. Whatever order you merge piles i..j in, the final merge always costs the total weight of i..j:
def min_merge_cost(piles):
n = len(piles)
if n <= 1:
return 0
prefix = [0] * (n + 1)
for i, p in enumerate(piles):
prefix[i + 1] = prefix[i] + p
dp = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
best = min(dp[i][k] + dp[k + 1][j] for k in range(i, j))
dp[i][j] = best + prefix[j + 1] - prefix[i]
return dp[0][n - 1]
print(min_merge_cost([4, 1, 1, 4])) # 18Thinking Backwards: The Last Operation in an Interval
In burst balloons, the balloon you burst first changes its neighbors, so the two sides are not independent. Instead, choose k as the last balloon burst in the open interval (left, right). When k bursts, its neighbors are left and right, so it gains vals[left] * vals[k] * vals[right]. The subproblems (left, k) and (k, right) are then independent.
Circular Sequences by Doubling the Array
If the piles sit in a circle, append a copy of the array to itself (a = piles + piles). Every rotation of the circle is now a contiguous window of length n in a. Run the same DP on a and return min(dp[i][i + n - 1] for i in range(n)).
Knuth's Optimization: From O(n^3) to O(n^2)
Knuth's optimization applies when the cost satisfies the quadrangle inequality and is monotone over nested intervals. Interval sums satisfy both conditions. Under these conditions, the best split obeys opt[i][j-1] <= opt[i][j] <= opt[i+1][j]. If you search k only inside that window, the total work over all intervals of one length is O(n), so the whole algorithm runs in O(n²).
Exercise
Why does burst balloons choose k as the last balloon burst rather than the first?
Show answer
If k bursts last in (left, right), its neighbors at that moment are fixed as left and right. That makes its gain known and makes the two sides independent subproblems. If k burst first, the neighbors of later balloons would depend on choices made on the other side.
Exercise
For the circular version of pile merging with n piles, what is the time complexity of the doubled-array DP without Knuth's optimization?
Show answer
O(n³). Intervals of length at most n in an array of size 2n give O(n²) states, and each state tries O(n) splits.