Interval (Range) DP
State is a contiguous range [l, r]; the answer is built by choosing a split point or the last element removed inside the range.
You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.
Overview
Interval DP applies when a problem on a contiguous segment [l, r] decomposes into problems on strictly shorter segments. The state is dp[l][r], there are O(n²) states, and the transition usually tries every split k in [l, r) (O(n)), giving O(n³) — hence typical constraints n ≤ 100–500. Some transitions are O(1) (palindromic substrings: dp[l][r] = s[l]==s[r] and dp[l+1][r-1]), giving O(n²) and allowing n ≤ 5000.
The crucial modelling trick is choosing what the split means. In Matrix Chain Multiplication k is the position of the last multiplication. In Burst Balloons k is the last balloon burst in (l, r) — thinking "first" instead of "last" fails because bursting a balloon changes the neighbours of the rest, while bursting it last means its neighbours are exactly l and r. Other members: Minimum Cost to Merge Stones, Stone Game, Strange Printer, Remove Boxes (with an extra dimension), Palindrome Partitioning II, Optimal BST, Minimum Score Triangulation.
Iteration order is by increasing interval length, so all sub-intervals are ready; top-down Memoization (Top-Down DP) is often cleaner because the order is implicit.
Intuition
A mental model before the formal terms.
Think of a long strip of paper you must cut into pieces, where every cut costs something depending on the piece being cut. The cheapest way to reduce the whole strip is: pick the very first cut somewhere, then optimally cut each of the two halves. Since the halves are independent, you only need the best cost of every possible sub-strip — a table indexed by (left end, right end).
Burst Balloons inverts the order: instead of "what happens first" ask "which balloon is left standing last in this range". Once you decide that, the range splits cleanly into two independent sub-ranges, and the last balloon's reward is computed against the fixed boundary balloons l and r.
How it works
- State:
dp[l][r]= answer for the segment[l, r](inclusive or exclusive boundaries — pick one and be consistent; exclusive boundaries with padding sentinels simplify Burst Balloons). - Transition:
dp[l][r] = best over k in (l, r) of dp[l][k] + dp[k][r] + cost(l, k, r)— or a directO(1)step in from both ends when the structure is palindrome-like. - Base cases: empty or single-element intervals:
dp[i][i] = 0(no work) or1/true(single char is a palindrome). - Order: for
lenfrom 2 ton, forlfrom 0 ton - len:r = l + len - 1. Table: 2D, onlyl ≤ rused. Answer:dp[0][n-1](ordp[0][n+1]with padding). - Optimization: Knuth's optimization brings some
O(n³)interval DPs toO(n²)when the optimal split is monotone; rarely needed in interviews.
Why it works
Whatever the optimal sequence of operations on [l, r], one operation is last (or first), and it splits the range into pieces whose operations are independent of each other. Each piece must be handled optimally (otherwise substitute a better handling), so trying every last operation and recursing is exact.
Increasing-length order is a topological order since every transition reads strictly shorter intervals.
Recognition
How to tell a problem wants this.
- Operations on a contiguous segment that remove/merge/cut elements and change the structure for the rest ("burst", "merge adjacent piles", "remove boxes", "parenthesize").
- Questions about substrings or subarrays where the answer for
[l, r]depends on[l+1, r-1]or on a split inside. - Constraints
n ≤ 100–500(cubic) orn ≤ 5000with anO(1)transition. - Two-player games on a line of items where players take from the ends (Stone Game / Predict the Winner).
Interactive visualization
Play, step, change the input. ← → and space work too.
Showing the closely related Matrix Chain Multiplication visualization.
| A1 | A2 | A3 | |
|---|---|---|---|
| A1 | 0 | · | · |
| A2 | · | 0 | · |
| A3 | · | · | 0 |
1dp[i][i] = 02for len in 2 .. n:3 for i in 1 .. n-len+1: j = i+len-14 dp[i][j] = ∞5 for k in i .. j-1:6 cost = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j]7 if cost < dp[i][j]: dp[i][j] = cost; split[i][j] = k8return dp[1][n]Pseudocode
1# burst balloons: pad nums with 1 on both ends2# dp[l][r] = max coins from bursting everything strictly between l and r3for len in 2..n+1:4 for l in 0..n+1-len:5 r = l + len6 for k in l+1..r-1: # k = last balloon burst in (l, r)7 dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r] + a[l]*a[k]*a[r])8return dp[0][n+1]Implementations
1import math2 3# Interval DP: the state is a RANGE [i, j], and the transition picks a split4# point or peels an end. Ranges are solved shortest-first, so every sub-range5# a transition needs is already final.6# Representative example: burst balloons, plus longest palindromic subsequence.7 8 91 · dp[i][j] = best score obtainable from the open interval (i, j)10def burst_balloons(input_nums: list[int]) -> int:11 # Pad with 1s so the boundary cases need no special handling12 nums = [1] + list(input_nums) + [1]13 n = len(nums)14 dp = [[0] * n for _ in range(n)]15 162 · Grow by interval length, so shorter ranges are always ready17 for length in range(2, n):18 for i in range(n - length):19 j = i + length203 · k is the LAST balloon burst inside (i, j), so i and j survive21 best = 022 for k in range(i + 1, j):23 best = max(best, dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j])24 dp[i][j] = best25 return dp[0][n - 1]26 27 284 · Same shape, different transition: peel the two ends instead of splitting29def longest_palindromic_subsequence(s: str) -> int:30 n = len(s)31 if n == 0:32 return 033 dp = [[0] * n for _ in range(n)]34 for i in range(n):35 dp[i][i] = 136 for length in range(2, n + 1):37 for i in range(n - length + 1):38 j = i + length - 139 dp[i][j] = dp[i + 1][j - 1] + 2 if s[i] == s[j] else max(dp[i + 1][j], dp[i][j - 1])40 return dp[0][n - 1]41 42 435 · Minimum cost to merge adjacent stones — the same length-first sweep44def merge_stones(a: list[int]) -> int:45 n = len(a)46 if n <= 1:47 return 048 prefix = [0] * (n + 1)49 for i, x in enumerate(a):50 prefix[i + 1] = prefix[i] + x51 dp = [[0] * n for _ in range(n)]52 for length in range(2, n + 1):53 for i in range(n - length + 1):54 j = i + length - 155 best = math.inf56 for k in range(i, j):57 best = min(best, dp[i][k] + dp[k + 1][j])58 dp[i][j] = int(best) + prefix[j + 1] - prefix[i] # plus the cost of this merge59 return dp[0][n - 1][1] + list(input_nums) + [1]copies and pads without touching the caller's list.- The inner minimisation accumulates into a local
bestand writesdp[i][j]once, which avoids repeated list indexing in the innermost loop. math.infis a float, soint(best)converts back before the integer arithmetic inmerge_stones— using a large integer sentinel would avoid the round trip.- The palindromic variant uses a conditional expression for the two-branch recurrence, keeping the body to one line.
range(n - length + 1)andrange(n - length)differ by one because the burst-balloons interval is open(i, j)while the palindromic one is closed[i, j]— a genuine source of off-by-one errors.
The n^3 triple loop is pure Python and is the slowest entry in this group; functools.lru_cache on a recursive form is often clearer at the same cost.
[[0] * n for _ in range(n)]is mandatory;[[0] * n] * naliases one row n times.functools.lru_cacheon a recursivesolve(i, j)expresses interval DP very naturally and handles the ordering automatically — at the cost of O(n^2) cache entries and O(n) recursion depth.math.infis a float; mixing it into an integer table means the table becomes float-typed unless converted back.- Hoisting
dp[i]into a local before the innermost loop is the usual CPython speed-up for a 2D DP.
- Aliasing rows with
[[0] * n] * n. - Leaving
math.infin an integer table so downstream comparisons become float comparisons. - Mixing the open-interval and closed-interval range bounds between the two variants.
- The infinity sentinel is cleanest in JS/TS (
Infinity) and Python (math.inf, though it is a float); C++ needsINT_MAXplus the discipline never to add to it before overwriting. - 2D table construction aliases by default in Python (
[[0]*n]*n) and JS/TS (fillwith an array), and never in C++. - Padding the input: Python
[1] + list(x) + [1]and JS/TS[1, ...x, 1]are one-liners that copy; C++ takes the vector by value and inserts, which is the same idea spelled differently. - Only Python offers a genuinely different formulation via
functools.lru_cacheon a recursivesolve(i, j), which handles the length-first ordering implicitly rather than requiring the loop discipline.
Complexity
Knuth or divide-and-conquer optimization can reduce O(n³) to O(n²) when the split point is monotone.
Compare growth rates in the Complexity Explorer →When to use — and when not to
- Contiguous ranges that decompose by a split point or by peeling both ends.
- Parenthesization / merge order problems (matrix chain, merging stones, polygon triangulation).
- Palindrome-structure questions on substrings.
nis large (≥ 10^4) —O(n²)states alone are too many; look for a greedy, a stack, or a linear DP.- Segments are independent and never interact — a 1D (Linear) DP over prefixes suffices.
- The operation does not change the structure for remaining elements — then order does not matter and a simpler formulation exists.
Alternatives
Common mistakes
- Filling the table in
l, rorder instead of by length, sodp[l][k]ordp[k][r]is still zero when read. - Choosing the "first" operation as the split when the structure requires "last" (Burst Balloons).
- Off-by-one in inclusive vs exclusive boundaries; mixing the two within one solution.
- Forgetting boundary sentinels (padding with 1s) and special-casing the edges instead.
Interview patterns
- Burst Balloons, Matrix Chain Order, Minimum Cost to Merge Stones, Minimum Score Triangulation.
- Longest Palindromic Subsequence, Palindromic Substrings count, Palindrome Partitioning II.
- Stone Game / Predict the Winner (score difference DP on
[l, r]). - Strange Printer, Remove Boxes (interval DP with an extra "count of equal on the right" dimension).
- Coin ChangeIntermediate
Example problems
Check yourself
Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.
How do you decide between BFS, DFS, Dijkstra, and Dynamic Programming?
Show a strong answerHide the answer
A strong candidate reframes the question: each of these answers a specific question about a specific kind of graph. What am I being asked — reachability, a shortest path, an ordering, an optimal value — and what does the graph look like — weighted or not, cyclic or not, explicit or implicit (states in a puzzle are a graph too).
Breadth-First Search (BFS) answers "fewest edges" and explores by distance layers; it is optimal only when every edge costs the same. Depth-First Search (DFS) answers structural questions — does a path exist, are there cycles, what is a topological order, which components exist — and is the natural backbone of backtracking. Dijkstra's Algorithm is BFS generalized to non-negative weights: replace the queue with a Priority Queue and you are done; its correctness argument breaks the moment a negative edge appears. Dynamic Programming is shortest (or best) path on a DAG: the subproblem dependency graph must be acyclic so subproblems can be solved in one topological pass.
The unifying view: all four are "process states in a valid order and relax transitions". BFS orders by hop count, Dijkstra by tentative distance, DP by topological order of subproblems, DFS by discovery. If the state graph has cycles and weights, you cannot use DP and need Dijkstra; if it is acyclic, DP is simpler and linear; if weights are uniform, BFS is both simpler and faster than Dijkstra.
Full interview question, with follow-ups and red flags →Interviewers ask about space complexity. When does it actually matter, and how do you reason about it?
Show a strong answerHide the answer
Space complexity counts auxiliary memory beyond the input: explicit structures (hash maps, DP tables), the recursion stack, and sometimes the output itself if it is large. A strong candidate lists all three; the recursion stack is the one people forget — recursive DFS on a tree is
O(h), and on a degenerate treeh = n.It matters in three situations. Memory limits: a
10^5 × 10^5DP table is10^{10}cells — impossible — so you need a 1D rolling array or a different state. Streams: if data is unbounded you must keepO(k)orO(1)state (top-kwith a heap, reservoir sampling). Explicit requirements: "in place" or "O(1)extra space" forces two pointers, index marking, or bit tricks instead of a hash set.The standard reductions: a 2D DP that only reads the previous row becomes two rows (Grid DP, Longest Common Subsequence); a hash set used for "seen" can become a sort plus adjacent comparison when reordering is allowed; recursion becomes an explicit loop; and a full array of prefix sums becomes a running sum when only the current prefix is needed. They also weigh the tradeoff honestly: an
Full interview question, with follow-ups and red flags →O(n)hash map that turnsO(n^2)intoO(n)is almost always worth it.Edge weights are only 0 and 1. What changes?
Show a strong answerHide the answer
0-1 BFS with a deque: push 0-cost neighbours to the front, 1-cost to the back.
Full interview question, with follow-ups and red flags →O(V + E)instead of Dijkstra's log factor.