Stone Merging
Fill an interval-DP triangle by trying every final split and adding the sum of the merged interval.
Core idea
The final operation on interval i through j must merge two adjacent already-complete subintervals. Try every split point and add the fixed total weight of the interval.
Read the visualization
The upper-triangular table fills by increasing interval length. Each highlighted cell compares split candidates from two smaller cells plus a prefix-sum interval cost.
| 4 | 1 | 3 | 2 | |
|---|---|---|---|---|
| 4 | 0 | |||
| 1 | 0 | |||
| 3 | 0 | |||
| 2 | 0 |
Initialize every one-pile interval with zero merge cost.
Complexity and tradeoffs
Time: O(n^3). Space: O(n^2). Each interval considers all split points; prefix sums make its merge cost O(1).
Where it fits
Interval DP appears in matrix-chain multiplication, polygon triangulation, optimal parsing, and burst-style games. Faster optimizations require additional structure in the split costs.