Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Dynamic Programming

Interval DP

O(n³)

dp[i][j] = best over all k of (left part + right part + cost of combining); fill by increasing length.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn first1D / Fibonacci-styleUnlocksPalindromic Substrings
0%
mastery
0/9 solved
IdeaPracticeTemplatePitfalls

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.

  1. 1Brute force

    Try every order of operations: factorial.

  2. 2The observation

    Order explosion: the result of a range depends only on the range (and maybe which element is last).

  3. 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

Next · Foundation

Stone Game

dp[l][r] = best score difference; (the parity observation also answers it).

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

  • Knuth's OptimisationFor interval DP dp[i][j] = min_k dp[i][k] + dp[k+1][j] + C(i, j) where opt(i, j-1) <= opt(i, j) <= opt(i+1, j), restrict k to that window for O(n^2).

Resources

  • DP lecture notesErrichto · video
  • AtCoder Educational DP ContestAtCoder · sheet
  • Introduction to DPUSACO Guide · course
  • Dynamic Programming PatternsLeetCode discuss · article
  • Dynamic Programming playlistAditya Verma · video
  • Bitmask DPUSACO Guide · course
  • Digit DPCodeforces · blog
Grid / Matrix DPState Machine DP (Stocks)
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial