Burst Balloons

Pattern: Partition and interval DP

hard

Bursting balloon k earns left × k × right with its current neighbours. Pad with 1s, and let dp[i][j] be the most coins from bursting everything strictly between i and j; choose the balloon burst last, whose neighbours are then i and j.

The problem

Bursting balloon i earns nums[i-1] × nums[i] × nums[i+1] with its current neighbours (a missing neighbour counts as 1). Return the most coins from bursting all of them.

Think first

  • Why is choosing the first balloon awkward?
  • What is fixed about the last balloon in a segment?
  • What do the padded 1s do?

Hints

    Brute force, and why it fails

    Try every order: n!.

    The key insight

    Fixing the last balloon makes the two sides independent, which turns the problem into interval DP.

    Dry run

    [3, 1, 5, 8] padded to [1, 3, 1, 5, 8, 1].

    Step 1 of 2

    The plan

    a = [1] + nums + [1]
    dp[i][j] = max over k in (i, j) of dp[i][k] + dp[k][j] + a[i] a[k] a[j]
    

    Complexity

    Time
    O(n³)
    Space
    O(n²)

    Segments times split points.

    Edge cases

    • One balloon.
    • Zeros.
    • Two balloons.
    I've built my logic, show the code

    Java

    class Solution {
        public int maxCoins(int[] nums) {
            int n = nums.length + 2;
            int[] a = new int[n];                   // pad with 1 at both ends
            a[0] = a[n - 1] = 1;
            for (int i = 0; i < nums.length; i++) a[i + 1] = nums[i];
            int[][] dp = new int[n][n];             // dp[i][j]: the most coins from bursting everything strictly between i and j
            for (int len = 2; len < n; len++)
                for (int i = 0; i + len < n; i++) {
                    int j = i + len;
                    for (int k = i + 1; k < j; k++) // k is the LAST balloon burst between i and j, so its neighbours are i and j
                        dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[k] * a[j]);
                }
            return dp[0][n - 1];
        }
    }

    C++

    class Solution {
    public:
        int maxCoins(vector<int>& nums) {
            int n = static_cast<int>(nums.size()) + 2;
            vector<int> a(n, 1);                    // pad with 1 at both ends
            for (size_t i = 0; i < nums.size(); i++) a[i + 1] = nums[i];
            vector<vector<int>> dp(n, vector<int>(n, 0));   // the most coins from bursting everything strictly between i and j
            for (int len = 2; len < n; len++)
                for (int i = 0; i + len < n; i++) {
                    int j = i + len;
                    for (int k = i + 1; k < j; k++) // k is the LAST balloon burst between i and j, so its neighbours are i and j
                        dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[k] * a[j]);
                }
            return dp[0][n - 1];
        }
    };

    Python

    class Solution:
        def maxCoins(self, nums: list[int]) -> int:
            a = [1] + nums + [1]                    # pad with 1 at both ends
            n = len(a)
            dp = [[0] * n for _ in range(n)]        # the most coins from bursting everything strictly between i and j
            for length in range(2, n):
                for i in range(n - length):
                    j = i + length                  # k is the LAST balloon burst between i and j
                    dp[i][j] = max(dp[i][k] + dp[k][j] + a[i] * a[k] * a[j] for k in range(i + 1, j))
            return dp[0][n - 1]

    Common mistakes

    • Choosing the first balloon.
    • Including the ends in the burst set.

    Similar problems