Partition and interval DP
Interval DP uses the state (i, j): the best for the segment from i to j. Try every way to split it, often by choosing which element is handled last (the last matrix multiplication, the last balloon burst, the root of a search tree), and combine the two sides. Fill by increasing segment length; it is usually O(n³).
How to recognise it
- A sequence where the order of combining or cutting matters.
- Choosing a split point, a root, or the last element.
- Two players taking from the ends.
Segments
for length in 2..n:
for i, with j = i + length - 1:
dp[i][j] = best over k of dp[i][k] + dp[k][j] + cost(i, k, j)
| Problem | Split by |
|---|---|
| matrix chain multiplication | the last multiplication |
| burst balloons | the last balloon burst |
| minimum cost to cut a stick | the first cut |
| optimal BST | the root |
| boolean parenthesization | the last operator (count true and false ways) |
| palindrome partitioning II | the last palindromic piece (1D) |
| optimal game strategy | take the left end or the right end |
Common mistakes
- Filling by i and j directly, so shorter segments are not ready.
- Including the segment's endpoints in the cost when the problem excludes them (balloons, sticks).
Problems
- Optimal Strategy for a Gamemediumguide ready
- Matrix chain multiplicationhardguide ready
- Burst Balloonshardguide ready
- Palindrome Partitioning IIhardguide ready
- Minimum Cost to Cut a Stickhardguide ready
- Word Wrap Problemhardguide ready
- Egg Dropping Problemhardguide ready
- Optimal Binary Search Treehardguide ready
- Boolean Parenthesization Problemhardguide ready