PrepStack · Markdown HTML

Interval dynamic programming

Dynamic programming · HTML

Formal shape

dp[l][r] describes closed interval [l,r]. In increasing length order, dp[l][r]=Aggregate_k(Combine(dp[l][k],dp[k+1][r],local(l,k,r))) or an endpoint-removal recurrence.

Recognition signals

False friends

SignalWhy it is different
Selecting independent external intervalsEndpoint greedy or weighted-interval DP is more direct when the intervals are input objects rather than subranges.
An arbitrary subset with holesTwo endpoints cannot represent removed interior choices; use a bitmask or richer state.
A merge with a proven greedy orderIf an exchange rule applies, avoid an unnecessary O(n³) interval table.

Core invariant

Before intervals of length len are processed, every shorter dependency is correct; dp[l][r] enumerates every legal final action for that interval.

State, transition, and processing order

ItemDefinition
StateAn optimum/count for closed endpoints (l,r), optionally with split[l][r] for reconstruction.
TransitionEnumerate the final split k or endpoint pair/removal and combine shorter interval answers with local cost.
Frontier / orderIncreasing interval length; within a length, choose l and set r=l+length-1.

Python skeleton

def min_merge_cost(values):
    n = len(values)
    if n <= 1: return 0
    prefix = [0]
    for x in values: prefix.append(prefix[-1] + x)
    dp = [[0] * n for _ in range(n)]
    for length in range(2, n + 1):
        for l in range(n - length + 1):
            r = l + length - 1
            total = prefix[r + 1] - prefix[l]
            dp[l][r] = min(
                dp[l][k] + dp[k + 1][r] + total
                for k in range(l, r)
            )
    return dp[0][n - 1]

Correctness sketch

  1. Intervals of length zero or one require no merge, establishing the base.
  2. Every full merge structure has a unique final split k whose two children are shorter intervals.
  3. By induction both children are optimal; enumerating every k and taking the best neither misses a structure nor exceeds the true optimum.

Complexity

TimeSpaceParameters
Typically O(n³), potentially O(n²) with proven transition optimizationO(n²)n is sequence length; each of O(n²) intervals tests O(n) splits.

Edge cases

Error patterns

PatternWhy it fails / fix
Filling by raw l/r orderA large interval may read an unfinished short one; length order is the dependency topology.
Overlapping or missing split rangesWith left [l,k], right must be [k+1,r], for k from l through r-1.
Double-counting the local totalSpecify whether a cost belongs to the final merge or was already included by children.
Claiming O(n²) too quicklyThere are O(n²) intervals and ordinarily O(n) split candidates per interval.

Selection and exclusion rules

RelationTemplateBoundary
contrastInterval sorting greedyExternal interval selection follows endpoints rather than internal parenthesization.
alternativeCenter expansion / Manacher palindromeOnly a palindrome radius or longest palindromic substring is needed.

Original micro-example

PartDetail
Resultdp[0][2]=9, with the final split after the second pile.
SetupAdjacent piles weigh [2,1,3]; merging two costs their combined weight.
TraceMerge 2+1 for 3, then merge with 3 for 6, totaling 9; the other order costs 4+6=10.

Recall prompts

My notes