Interval DP
O(n³)
dp[i][j] = best over all k of (left part + right part + cost of combining); fill by increasing length.
Learn first1D / Fibonacci-styleUnlocksPalindromic Substrings
0%
mastery
0/9 solved
0/9 solved
Recognise it
The signal
Solve a range [i..j] by trying every split k: burst balloons, matrix chain, cut a stick, merge stones, two players taking from ends.
1Brute force
Try every order of operations: factorial.
2The observation
Order explosion: the result of a range depends only on the range (and maybe which element is last).
3Why it works
dp[l][r] is final for every shorter range before any longer range is computed.
Reach for it when
- +two players take from ends
- +cost of cutting/merging segments
Not when
- ×n in the thousands, so O(n³) is too slowuse Knuth optimisation or a different model
- ×The pieces aren't contiguous segmentsuse A different DP state
Practice
0/9 solved
- Stone GameFoundationFoundationMedium
- Removal GameApply it · Same game, actual scores at n = 5000.Apply itCSESMedium
- Minimum Score Triangulation of PolygonOne change · Split by a third vertex.One changeMedium
- Guess Number Higher or Lower IITwists · Minimax over the split.TwistsMedium
- Minimum Cost to Cut a StickBranches · Range indices are cut positions.BranchesHard
- Burst BalloonsAdvanced · Choosing the first action creates dependencies.AdvancedHard
- Strange PrinterMastery checkMastery checkHard
- Rectangle CuttingCSESMedium
- Empty StringCSESMedium
Pitfalls
Classic bugs
- !Picking the FIRST action in the range instead of the LAST (burst balloons needs 'k is burst last').
- !Fill by increasing length, not plain row order — otherwise you read sub-ranges not yet computed.
Looks like it, isn't
- Split Array Largest Sum needs Binary Search on the Answer
Why
DP over partitions works (O(k n2)), but 'minimise the largest' points to binary search with greedy check. - Stone Game needs Combinatorial Game Theory (minimax, Nim, Sprague-Grundy)
Why
Interval DP works, but with an even number of piles the first player can always take all odd or all even indices. source ↗ - Minimum Number of Taps to Open to Water a Garden needs Greedy
Why
Convert each tap to its reach interval; the jump-game-ii farthest-reach greedy covers [0, n] in O(n). source ↗
Go deepervariants and techniques · resources
Variants and techniques