Sub topic 11
Constant-Time Interval Costs with Prefix Sums
This chapter assumes you know the split-point recurrence, filling the table by interval length, and the O(n^3) cost analysis from earlier chapters. You should also be comfortable with Python list indexing.
Many interval recurrences add a cost that depends on the whole range, on top of the best split:
dp[i][j] = min over k of (dp[i][k] + dp[k+1][j]) + cost(i, j)Often cost(i, j) is the sum of the elements from index i to index j. If you compute that sum with a loop every time, each state does O(n) extra work. That extra work is added once per state, beside the split loop, so the asymptotic time stays at O(n^3). It still roughly doubles the inner work. It becomes a real problem when you write it inside the k loop, which is an easy mistake to make, because then the algorithm runs in O(n^4).
The Prefix Sum Idea
A prefix sum array stores running totals. Define prefix[t] as the sum of the first t elements, so prefix[0] = 0. Then the sum of the inclusive range a[i..j] is:
sum(i, j) = prefix[j + 1] - prefix[i]You build the prefix array once in O(n). After that, every range sum is one subtraction.
Implementation
def build_prefix(a):
prefix = [0] * (len(a) + 1)
for i, x in enumerate(a):
prefix[i + 1] = prefix[i] + x
return prefix
def range_sum(prefix, i, j):
# Inclusive bounds: a[i] + a[i+1] + ... + a[j]
return prefix[j + 1] - prefix[i]
a = [4, 1, 1, 4]
prefix = build_prefix(a)
print(prefix) # [0, 4, 5, 6, 10]
print(range_sum(prefix, 1, 2)) # 2
print(range_sum(prefix, 0, 3)) # 10
# Check against the naive loop for every interval.
n = len(a)
for i in range(n):
for j in range(i, n):
assert range_sum(prefix, i, j) == sum(a[i:j + 1])
print("all intervals match")Inside an interval DP, compute the cost once per state, outside the k loop:
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]The next chapter uses exactly this shape for stone merging.
Common Errors
| Mistake | Symptom | Fix |
|---|---|---|
| Using `prefix[j] - prefix[i]` with inclusive `j` | Every sum is missing its last element | Use `prefix[j + 1]`, or switch the whole DP to half-open `[i, j)` |
| Sizing `prefix` as `n` instead of `n + 1` | `IndexError` on the full range | Allocate `n + 1` slots with `prefix[0] = 0` |
| Mixing inclusive and exclusive conventions | Off-by-one answers on some lengths only | Choose one convention for `dp` and for `cost`, and write it in a comment |
| Computing the sum inside the k loop | O(n^4) running time | Hoist the cost out of the split loop |
Precomputing a Cost Table
If the cost has no inverse but can be extended one element at a time, fill cost[i][j] = combine(cost[i][j-1], a[j]) in O(n^2) before the DP starts. That still gives O(1) lookups and adds no asymptotic cost.
Exercise
For a = [3, 7, 2, 5, 1], build the prefix array and use it to find the sum of a[1..3].
Show answer
prefix = [0, 3, 10, 12, 17, 18]. sum(1, 3) = prefix[4] - prefix[1] = 17 - 3 = 14, which matches 7 + 2 + 5.
Exercise
A DP stores half-open intervals, where dp[i][j] covers a[i..j-1]. What is the range-sum formula, and what is the cost of an empty interval?
Show answer
sum = prefix[j] - prefix[i]. An empty interval has i == j, so its sum is prefix[i] - prefix[i] = 0.
Exercise
Someone writes min(dp[i][k] + dp[k+1][j] + sum(a[i:j+1]) for k in range(i, j)). What is the overall time complexity, and how do you fix it?
Show answer
The slice sum costs O(n) for each of the O(n) choices of k, and there are O(n^2) states, so the total is O(n^4). Compute prefix[j+1] - prefix[i] once, outside the min, which brings the total back to O(n^3).