AlgoMaster Logo

Burst Balloons

Ashish

Ashish Pratap Singh

hard

Problem Description

Solve it on LeetCode

Approaches

1. Dynamic Programming (Memoization)

Intuition:

The key observation in this problem is that the order of bursting balloons does not matter globally, but it matters locally for a particular subproblem. We can think of each balloon as contributing coins when it is the last balloon of any subsequence to be burst. We can use dynamic programming to solve this by dividing the problem into subproblems, where we consider bursting a balloon last in a specific range, and compute the maximum coins obtainable.

Let's use a dynamic programming table dp where dp[left][right] represents the maximum coins obtainable by bursting all the balloons between index left and right (exclusive).

Steps:

  1. Use an auxiliary array nums with additional boundary balloons (values 1 at both ends), because the edge balloons behave as if they are surrounded by 1.
  2. Use a helper function maxCoins(left, right) which uses memoization to store already computed results.
  3. For each balloon i between left and right, calculate the coins gained by making it the last burst balloon, plus the resulting coins from the two partitions (left, i) and (i, right).
  4. Memorize and return the maximum result for subproblem maxCoins(left, right).

Code:

2. Dynamic Programming (Tabulation)

Intuition:

This approach is similar to the memoization approach, but we fill up our table in a bottom-up manner instead. We compute the solution for smaller subproblems first and use them to build up the solution for larger subproblems.

Steps:

  1. We still prepare an auxiliary array nums with boundary balloons.
  2. Initialize a DP table where dp[i][j] will represent the maximum coins obtainable by bursting all the balloons between index i and j (exclusive).
  3. Use a nested loop structure to compute dp[i][j] for subproblems of increasing size.
  4. For each k in current subproblem, compute maximum coins by bursting the k-th balloon last in its section.

Code: