Burst balloons

Dynamic Programming MCM DP Hard

Given n balloons, indexed from 0 to n - 1, each balloon is painted with a number on it represented by an array nums. Burst all the balloons.


If the ith balloon is burst, the coins obtained are nums[i - 1] * nums[i] * nums[i + 1]. If i - 1 or i + 1 goes out of bounds of the array, treat it as if there is a balloon with a 1 painted on it.


Return the maximum coins that can be collected by bursting the balloons wisely.

Examples:

Input : nums = [3, 1, 5, 8]

Output : 167

Explanation :

nums = [3, 1, 5, 8] --> [3, 5, 8] --> [3, 8] --> [8] --> []

coins = 3*1*5  +  3*5*8  + 1*3*8 + 1*8*1 = 167.

Input : nums = [1, 2, 3, 4]

Output : 40

Explanation :

nums = [1, 2, 3, 4] --> [1, 2, 4] --> [1, 4] --> [4] --> []

coins = 2*3*4 + 1*2*4 + 1*1*4 + 1*4*1 = 40.

Input : nums = [1, 5]

Constraints

  • 1 <= n <= 300
  • 1 <= nums[i] <= 100

Hints

  • Define dp[i][j] as the maximum coins obtained by bursting balloons in the range [i, j].
  • "The final burst at k contributes nums[i-1] * nums[k] * nums[j+1] coins. Solve subproblems first, then use them to compute dp[i][j]."

Company Tags

Freshworks HCL Technologies Pinterest Roblox KPMG Optum Goldman Sachs IBM Seagate Technology Target Unity Technologies Byju's Databricks Zynga Broadcom Shopify Cerner Uber Salesforce JPMorgan Chase Square Stripe Robinhood Mastercard Red Hat Google Microsoft Amazon Meta Apple Netflix Adobe