Bonus Day 117HardLeetCode

Burst Balloons

Burst balloons in the best order, where each burst pays the product of the balloon and its current neighbours, and find the most coins you can collect.

Pattern
Interval DP, choose the last one
Time box
40 min
Time
O(n³)
Space
O(n²)

Try it first — give it 40 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    There are n balloons in a row, and balloon i has the number nums[i] painted on it. You burst them one at a time, in any order you like. Bursting balloon i pays left × nums[i] × right coins, where left and right are the numbers on the balloons currently next to it. Once a balloon is gone, its neighbours become neighbours of each other. If there's no balloon on a side, treat that side as a 1.

    Burst them all and return the maximum coins possible. n is at most 300, and each number is 0 to 100.

    Example: nums = [3, 1, 5, 8] → 167. One best order is to burst 1, then 5, then 3, then 8:

    [3, 1, 5, 8]  burst 1 → 3·1·5 = 15
    [3, 5, 8]     burst 5 → 3·5·8 = 120
    [3, 8]        burst 3 → 1·3·8 = 24
    [8]           burst 8 → 1·8·1 = 8         total 167

    Start with brute force

    Try every order. There are n! orders. For n = 10, that's 3.6 million. For n = 300, it's beyond astronomical.

    You could memoise on the set of balloons still standing: that's 2ⁿ states. That handles n = 20 at most. We need a state that's much smaller than a set.

    Building the intuition

    This is the stage's interval DP problem. Interval DP means the state is a contiguous range [l, r], and you solve a range by splitting it at some point k into two smaller ranges. The hard part here is finding a split that actually makes the two halves independent.

    Attempt 1: choose the first balloon to burst. Say you burst k first in the range. Now k−1 and k+1 are neighbours. The left part and the right part touch each other, and every later burst near the middle depends on what's left on both sides. The two halves aren't independent. That's a dead end.

    Attempt 2: choose the last balloon to burst. Now ask: in the range (l, r), which balloon k goes last? Think about what that means.

    • Until the very end, k is still standing. Every balloon between l and k gets burst while k is a wall on its right. Every balloon between k and r gets burst while k is a wall on its left. The two sides never see each other. They're independent subproblems.
    • When k finally bursts, everything else in the range is already gone. So its neighbours are exactly l and r, the range's ends. It pays vals[l] · vals[k] · vals[r].

    So:

    dp[l][r] = max over l < k < r of  dp[l][k] + vals[l]·vals[k]·vals[r] + dp[k][r]

    where dp[l][r] means "the best coins from bursting every balloon strictly between l and r, while l and r are still standing".

    Why the padding? Put a 1 at each end: vals = [1] + nums + [1]. Those two fake balloons are never burst. They're exactly the "treat a missing side as 1" rule. The answer is dp[0][n+1]: burst everything between the two fake ends.

    The base case is free: if r = l + 1, there's nothing between them, so dp[l][r] = 0.

    The optimal approach

    1. vals = [1] + nums + [1], and let N = len(vals).
    2. dp is an N × N table of zeros.
    3. For length (that is, r − l) from 2 up to N − 1, and for each l with r = l + length inside the array:
      • dp[l][r] = max(dp[l][k] + vals[l]·vals[k]·vals[r] + dp[k][r]) over l < k < r.
    4. Return dp[0][N − 1].

    The subtle detail: fill order. dp[l][r] reads dp[l][k] and dp[k][r], which are both shorter ranges. So loop by gap length, short to long. If you loop l from 0 upward with r inside, you'll read cells that haven't been computed yet. (Looping l from right to left and r from left to right also works.)

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class Solution:
        def maxCoins(self, nums: List[int]) -> int:
            # Pad with the imaginary 1s at both ends, so every balloon has two neighbours.
            vals = [1] + nums + [1]
            n = len(vals)
            # dp[l][r] = best coins from bursting every balloon strictly between l and r,
            # while l and r themselves are still standing.
            dp = [[0] * n for _ in range(n)]
    
            for length in range(2, n):          # distance r - l; short gaps first
                for l in range(0, n - length):
                    r = l + length
                    best = 0
                    for k in range(l + 1, r):    # k = the LAST balloon burst in (l, r)
                        # When k goes last, its neighbours are exactly l and r.
                        best = max(best, dp[l][k] + vals[l] * vals[k] * vals[r] + dp[k][r])
                    dp[l][r] = best
    
            return dp[0][n - 1]

    Dry run

    nums = [3, 1, 5, 8] → vals = [1, 3, 1, 5, 8, 1] (indices 0–5). Each row shows the best "last balloon" k for that range.

    Range (l, r)Balloons insideBest k (value)Calculationdp[l][r]
    (0, 2)31 (3)0 + 1·3·1 + 03
    (1, 3)12 (1)0 + 3·1·5 + 015
    (2, 4)53 (5)0 + 1·5·8 + 040
    (3, 5)84 (8)0 + 5·8·1 + 040
    (0, 3)3, 11 (3)0 + 1·3·5 + dp[1][3] = 15 + 1530
    (1, 4)1, 53 (5)dp[1][3] + 3·5·8 + 0 = 15 + 120135
    (2, 5)5, 84 (8)dp[2][4] + 1·8·1 + 0 = 40 + 848
    (0, 4)3, 1, 51 (3)0 + 1·3·8 + dp[1][4] = 24 + 135159
    (1, 5)1, 5, 84 (8)dp[1][4] + 3·8·1 + 0 = 135 + 24159
    (0, 5)all4 (8)dp[0][4] + 1·8·1 + 0 = 159 + 8167

    Read the answer backwards: 8 goes last overall. Before that, in (0, 4), 3 goes last. Inside (1, 4), 5 goes last, and inside (1, 3), 1 goes first. That's the order 1, 5, 3, 8 from the example.

    Complexity

    • Time: O(n³). There are O(n²) ranges, and each one tries O(n) choices of k. For n = 300, that's about 4.5 × 10⁶ inner steps, which is fast.
    • Space: O(n²) for the table.

    Edge cases and traps

    • Thinking "first" instead of "last". This is the whole problem. If your recurrence picks the first balloon, the subproblems overlap through changing neighbours, and the DP is wrong.
    • Forgetting the padding. Without the 1s, the ends need special cases everywhere. Pad once and the formula works for every range.
    • Wrong fill order. Iterate by gap length, or by l descending with r ascending. Plain for l: for r: reads uncomputed cells.
    • Zeros. A 0 balloon pays nothing itself, and makes its neighbours' bursts pay nothing while it stands next to them. The DP handles it. [0, 0, 0] → 0.
    • Overflow. The worst case is about 300 bursts × 100³ = 3 × 10⁸, which fits in an int.

    Say it in the interview

    "Bursting changes who's adjacent, so choosing the first balloon couples the two sides. Instead I choose the last balloon k in a range. While k stands, the left and right parts are independent, and when it bursts, its neighbours are the range's fixed ends. I pad with 1s and define dp[l][r] as the best coins for everything strictly between l and r. That's the max over k of dp[l][k] plus vals[l]·vals[k]·vals[r] plus dp[k][r]. I fill by increasing range length. There are O(n²) states with O(n) transitions each, so O(n³) time and O(n²) space."

    Follow-ups interviewers ask

    • "Which other problems are interval DP?" Minimum Cost Tree From Leaf Values, Minimum Score Triangulation of Polygon, Strange Printer, Remove Boxes, and matrix-chain multiplication. The isPal[i][j] table version of Longest Palindromic Substring is a simpler cousin: its state is a range too, but it doesn't split. The tell: a range, and a choice that splits it into two independent ranges.
    • "Recover the order of bursts." Store the best k for each (l, r). Then recurse: build the order for (l, k), then for (k, r), then burst k last.
    • "Top-down instead?" Write best(l, r) with memoisation and the same recurrence. It's often easier to get right in an interview, and it has the same O(n³).

    Practise the same idea

    This is a bonus day of the free DSA plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free