Interval Dynamic Programming

Context: FIT2004_MOC · the fourth DP subproblem shape — DP[i][j] over a contiguous range , filled by increasing length, not by increasing index Parent Framework: Dynamic Programming

Quick Revision

  • 🎯 Objective: when a decision shrinks a range from either end or splits it at a cut point, the subproblem is the range itself ➔ cells, filled shortest-first.
  • 📦 Core Components: shrink-from-both-ends ➔ palindromes, the coin game | split-at- ➔ boolean parenthesisation, matrix chain multiplication.
  • ⚡ Key Constraint: the fill order is by range length, because reads strictly shorter ranges. A plain for i / for j loop reads unwritten cells and is the defining bug of this shape.

📝 How It Works

1. Recognising the Shape

  • The trigger ➔ every legal move removes an element from the left end, the right end, or splits the range in two. What survives is always a contiguous .
  • Subproblem count ➔ ranges ⟹ auxiliary space ; the table is upper-triangular (only is meaningful).
  • Cost ➔ if each range decides in (shrink); if each range tries every cut (split).
  • Answer ➔ — the full range — so no scan, unlike the anchored shapes (LIS, Maximum Subarray Sum).
  • Empty-range convention ➔ define for ; the shrink recurrences below index , which is empty when .
  • Contrast with LCS ➔ that grid indexes two prefixes of two sequences; this one indexes one range of one sequence. Same , different meaning — say which you are doing.

2. Shrink From Both Ends — Longest Palindromic Subsequence (applied P10a)

  • The structure ➔ is a palindrome iff and is a palindrome; an empty or single-element sequence is one.
  • The decision ➔ if the two ends match, use both; otherwise at least one is unusable, so drop each in turn and take the better.
  • Answer ➔ ; subproblems at ⟹ time and space.
  • Worked micro-example ➔ on the ends match at every level ⟹ , the whole sequence.

3. Substring Subsequence (applied P10b, P10c)

  • The longest palindromic substring is contiguous ➔ a different problem, and not solved by the DP above.
  • without DP ➔ a palindrome is symmetric about a middle, so try every centre and expand while the characters match: element-centres (odd lengths) gap-centres (even lengths), each expanding .
  • The two loops are separate ➔ odd uses a[i-j] == a[i+j] and reports ; even uses a[i-j+1] == a[i+j] and reports . Handling only one halves your answers on the wrong inputs.
  • [D] ➔ pad with a separator so every palindrome is odd-length, then store the radius of the largest palindrome centred at and reuse symmetry: for inside a known palindrome centred at , its mirror gives in unless the mirror reaches the boundary. Amortised argument: non-constant work always pushes the rightmost reach further right, and that reach moves times in total.

4. Adversarial Interval DP — the Coin Game (applied P9)

  • Problem ➔ a row of coins ; players alternately take the first or the last coin; both play optimally; you move first. Maximise your total.
  • Greedy fails ➔ on taking the larger end () exposes the ; taking the from the right guarantees you the whatever the opponent does ➔ Greedy Algorithm §2.
  • The state is the remaining range ➔ whatever has been taken, the rest is always a contiguous . DP[i][j] = {the maximum score obtainable playing FIRST on a[i..j]}.
  • The zero-sum flip ➔ if you take , your opponent then plays first on and scores , so you get everything else: . Taking gives .
  • becomes ➔ , because the sign reverses the comparison:
  • Keep it per cell ➔ precompute prefix sums so ➔ Maximum Subarray Sum §1. Without them each cell costs and the bound becomes .
  • Cost ➔ time and space; answer .

5. Split at a Cut Point — the Family (applied P19)

  • The trigger ➔ the range is not shrunk but divided, and the division point is itself a choice. DP[i][j] = best over all cuts k in [i, j-1] of (DP[i][k] combined with DP[k+1][j]).
  • Boolean parenthesisation ➔ count the parenthesisations of that evaluate to True. The key observation: some operator is evaluated last, so try each as the final one.
  • Two payloads, not one ➔ you need the True-count and the False-count of each subexpression, because can be made true by a False left operand: for .
  • The combination rules ➔ for : , ; for : , .
  • Cost ➔ ranges cut points ; the dimension is a constant .
  • Same shape, credit-level classic ➔ matrix chain multiplication (named in the wrap-up) is this recurrence with “number of scalar multiplications” as the payload and as the decision.

⚙️ Core Implementation

🔹 The coin game — fill by increasing range length

⚖️ Complexity

ProblemSubproblemsWork per subproblemTimeAuxiliary space
Longest palindromic subsequence
Longest palindromic substring (centres)— (not DP) per centre
Longest palindromic substring [D] radii amortised
Coin game (adversarial) with prefix sums
Boolean parenthesisation (split) cuts
  • The discriminator between and ➔ shrink recurrences look at successors; split recurrences look at cut points. Read the recurrence, not the problem statement.

📊 Exam Execution Trace & Applied Exercises

Manual Execution Trace — the coin game

Coins . Filled by increasing range length; .

LengthRange
each ————

Final Extracted Output: — matching the sheet. The opponent therefore scores .

  • Read the trap ➔ at the picks , i.e. taking the left coin . The larger end is , so the optimal first move is the smaller coin.
  • Read the fill order ➔ every row uses only values from shorter ranges; the length column is the dependency order.

Applied Exercise — kill the greedy strategy (P9a)

Problem: show that “always take the more valuable end” is not optimal.

Final Extracted Output: greedy vs optimal . The reusable move (LO1): in a two-player game the state is what remains, and what remains is a contiguous range — so the paradigm is interval DP, and the opponent’s optimum enters your recurrence with a minus sign.

⚠️ Common Mistakes

  • 💡 Index-major fill order ➔ for i / for j reads shorter ranges that have not been computed. Loop on length first, always.
  • 💡 Forgetting the flip in an adversarial DP ➔ ; keeping silently models a cooperative opponent.
  • 💡 Recomputing range sums ➔ where was asked for; prefix sums are up front.
  • 💡 Applying the subsequence recurrence to a substring question ➔ contiguity changes the problem; the DP above answers subsequence only ➔ §3.
  • 💡 Omitting the empty-range convention ➔ for is what makes correct on an adjacent matching pair.

🧠 Active Recall