Sub topic 1
Thinking in Intervals: What dp[i][j] Means
Many optimization problems on sequences share a shape. The best answer for a whole sequence can be built from the best answers for its contiguous pieces. Interval dynamic programming uses that shape. You store one answer per contiguous range and build long ranges out of shorter ones.
Prerequisites
This chapter assumes you already know:
- Basic dynamic programming, both memoization and tabulation
- Recursion
- Two-dimensional arrays
- Big-O time and space analysis
- How to read Python code
Recognizing an Interval Problem
A problem is a good fit for interval DP when all of these hold:
- The input is an ordered sequence, such as an array, a string, or a chain of matrices.
- Any operation you apply keeps neighbors as neighbors. You merge, split, or remove items, but you never reorder them.
- After one decision, what remains is one or two smaller contiguous ranges that you can solve independently.
Some typical clues are "adjacent", "parenthesize", "merge neighboring", and "subsequence of a string". If a solution can pick items from anywhere and rearrange them, you probably need a different state.
The State
The state dp[i][j] holds the best answer for the part of the sequence between positions i and j. Before you write any recurrence, write down one exact sentence that defines it. Most bugs in interval DP come from vague bounds.
There are two common conventions:
| Convention | Meaning of dp[i][j] | Single element | Empty range | Whole array of n items |
|---|---|---|---|---|
| Inclusive | items `i` through `j` | `i == j` | `i > j` | `dp[0][n-1]` |
| Half-open | items `i` through `j-1` | `j == i + 1` | `i == j` | `dp[0][n]` |
Either convention works. Mixing them in one solution does not. For example, if your recurrence reads dp[i][k] and dp[k+1][j], you are using inclusive bounds. If it reads dp[i][k] and dp[k][j], you are using half-open bounds.
The code below lists every interval of a short sequence under both conventions. Both give n(n+1)/2 non-empty ranges, which is how many states the table has to hold.
def inclusive_intervals(n):
return [(i, j) for i in range(n) for j in range(i, n)]
def half_open_intervals(n):
return [(i, j) for i in range(n) for j in range(i + 1, n + 1)]
seq = ["a", "b", "c", "d"]
n = len(seq)
for i, j in inclusive_intervals(n):
assert seq[i:j + 1] == seq[i:i + (j - i + 1)]
print(len(inclusive_intervals(n))) # 10
print(len(half_open_intervals(n))) # 10
print(n * (n + 1) // 2) # 10Python slices are half-open. So seq[i:j] matches the half-open state directly. The inclusive state needs seq[i:j + 1].
How Intervals Depend on Each Other
An interval's answer depends only on strictly shorter intervals inside it. This rule is what makes the table computable, and later chapters build on it.
Common Errors
- Off-by-one in the final answer. Reading
dp[0][n-1]from a half-open table, ordp[0][n]from an inclusive one. - An undefined empty range. Under inclusive bounds,
dp[i][i-1]comes up often. Decide what it means before you use it. - A state that hides extra information. If the answer for
[i, j]also depends on something outside the range, such as which neighbor is still present, thendp[i][j]alone is not enough. You need to redefine the range or add a dimension.
Exercise
You define dp[i][j] as the minimum cost to merge items i through j-1 into one item. Which convention is this, which entry holds the answer for an array of length n, and which entries describe a single item?
Show answer
It is the half-open convention. The answer is dp[0][n]. Single items are the entries dp[i][i+1] for i from 0 to n-1.
Exercise
For a sequence of length 6, how many non-empty contiguous intervals are there?
Show answer
6 * 7 / 2 = 21.