Burst Balloons

Burst Balloons : An In-Depth Explanation

The “Burst Balloons” problem challenges you to maximize the coins collected by strategically bursting balloons in a given array. Each balloon has a value, and bursting it earns coins equal to the product of its value and the values of its adjacent balloons.

If a neighboring index is out of bounds, it is treated as 1. The goal is to determine the optimal order to burst all balloons and achieve the maximum total coins.

Problem: 

You are provided with an array of integers, nums, of size n. Each element nums[i] represents a balloon with a certain integer value. Your task is to burst all the balloons.

When you burst the i-th balloon, you earn coins equal to nums[i - 1] * nums[i] * nums[i + 1]. If i - 1 or i + 1 goes beyond the boundaries of the array, consider the out-of-bounds value as 1.

Your goal is to determine the maximum coins you can collect by strategically bursting all the balloons.

Problem Breakdown

  1. Input: An array nums where each element represents a balloon’s value.
  2. Bursting Rule: Bursting the i-th balloon gives coins equal to nums[i-1] * nums[i] * nums[i+1]. Treat out-of-bounds values as 1.
  3. Goal: Find the maximum coins you can collect by bursting all balloons in the best order.
  4. Challenge: The order of bursting affects the total coins, making it a combinatorial problem.
  5. Approach: Use dynamic programming to calculate the maximum coins for subarrays and build up to the full solution.

Constraints:

  • n == nums.length
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100

Approach

  • Here’s the short approach without the formula:

    1. Add Boundary Balloons: Add two virtual balloons with value 1 at the start and end of the array.

    2. DP Table Setup: Create a 2D DP table dp[i][j] to store the maximum coins collectable from bursting balloons in the subarray nums[i] to nums[j].

    3. Recursive Calculation: For each subarray [i, j], determine the best balloon k to burst last. The maximum coins for the subarray will depend on the maximum coins for the left and right subarrays and the coins from bursting balloon k.

    4. Iterate Over Subarrays: Fill the DP table by considering all possible subarrays and lengths.

    5. Return Result: The maximum coins are in dp[0][n-1], where n is the length of the modified nums.

There are mainly three approach to solve this problem – 

  1. Brute Force 
  2. Dynamic Programming (Top-Down)
  3. Dynamic Programming (Bottom-up)

1. Brute Force (Recursion)

  • Time complexity: O(n∗2n)
  • Space complexity: O(n∗2n)

2. Dynamic Programming (Top-Down)

Time & Space Complexity
  • Time complexity: O(n^3)
  • Space complexity: O(n^2)

3. Dynamic Programming (Bottom-Up)

Time & Space Complexity
  • Time complexity: O(n^3)
  • Space complexity:

More Articles