Dynamic Programming · Pattern 9 of 12

Interval DP: solve every range, short to long

dp[i][j] is the answer for the range i..j. Try every split point k inside it and combine the two sides. Fill by range length so the smaller ranges are ready.

3 variations · 6 problems · 3 with animated walkthroughs

Watch the lesson

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

When to use it

  • The cost depends on the order you merge, multiply or remove things
  • Two players take from either end of an array
  • Brackets or the last operation split the range into a left and a right part

The template

Interval DP
// Matrix Chain: matrix i is arr[i - 1] x arr[i]; dp[i][j] = cheapest way to multiply i..j
int matrixMultiplication(int[] arr) {
    int n = arr.length;
    int[][] dp = new int[n][n];            // one matrix alone costs 0
    for (int len = 2; len < n; len++)      // number of matrices, short ranges first
        for (int i = 1; i + len - 1 < n; i++) {
            int j = i + len - 1;
            dp[i][j] = Integer.MAX_VALUE;
            for (int k = i; k < j; k++)    // the last multiplication is (i..k) x (k+1..j)
                dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j]);
        }
    return dp[1][n - 1];
}

// Burst Balloons: choose the LAST balloon k burst between l and r (both kept as walls)
int maxCoins(int[] nums) {
    int n = nums.length + 2;
    int[] a = new int[n];
    a[0] = a[n - 1] = 1;                   // pad with 1s
    for (int i = 0; i < nums.length; i++) a[i + 1] = nums[i];
    int[][] dp = new int[n][n];            // dp[l][r] = best coins for the balloons strictly between
    for (int len = 2; len < n; len++)
        for (int l = 0; l + len < n; l++) {
            int r = l + len;
            for (int k = l + 1; k < r; k++)
                dp[l][r] = Math.max(dp[l][r], dp[l][k] + dp[k][r] + a[l] * a[k] * a[r]);
        }
    return dp[0][n - 1];
}

The variations

  1. 1Split the range at k

    The last operation splits i..j into i..k and k + 1..j; try every k. Matrix Chain adds the cost of the final product. The Score of Students computes the set of all values every sub-expression can take, and Strange Printer merges a character with a later equal one.

  2. 2The last item removed

    When removing an item changes its neighbours, think backwards: pick the item removed last in the range. Its neighbours at that moment are the range borders, which never change.

    Now you solve
  3. 3Two players on a range

    Players take from either end. dp[i][j] = how much more the player to move can score than the other on a[i..j]. Player 1 wins if dp[0][n - 1] >= 0.

Mistakes to avoid

  • Looping i and j from 0 upwards: dp[i][j] then reads ranges that are not filled yet. Loop over the length first.
  • Burst Balloons: choosing the first balloon to burst makes the neighbours change; choose the last one, whose neighbours are the fixed walls l and r.
  • Predict the Winner: storing each player's score separately. Store the score difference for the player to move: max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]).
Next: Tree DP: answers from the children up →

Mark it done once you can write the template from memory and you have solved the worked examples yourself.