Tutorialspoint
Problem
Solution
Submissions

Burst Balloons

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to find the maximum coins you can collect by bursting balloons. You are given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

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

Return the maximum coins you can collect by bursting the balloons wisely.

Example 1
  • Input: nums = [3,1,5,8]
  • Output: 167
  • Explanation:
    Burst balloon with value 1, getting 3*1*5 = 15 coins.
    Remaining balloons: [3,5,8] Burst balloon with value 5, getting 3*5*8 = 120 coins.
    Remaining balloons: [3,8] Burst balloon with value 3, getting 1*3*8 = 24 coins.
    Remaining balloons: [8] Burst balloon with value 8, getting 1*8*1 = 8 coins.
    No balloons remain. Total coins = 15 + 120 + 24 + 8 = 167
Example 2
  • Input: nums = [1,5]
  • Output: 10
  • Explanation:
    Burst balloon with value 1, getting 1*1*5 = 5 coins.
    Remaining balloons: [5] Burst balloon with value 5, getting 1*5*1 = 5 coins.
    No balloons remain. Total coins = 5 + 5 = 10
Constraints
  • n == nums.length
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100
  • Time Complexity: O(n³)
  • Space Complexity: O(n²)
ArraysDynamic Programming FacebookAccenture
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use dynamic programming to solve this problem
  • Instead of considering which balloon to burst first, consider which balloon to burst last
  • Create a new array with 1s at the beginning and end to handle boundary cases
  • Define dp[i][j] as the maximum coins that can be obtained by bursting all balloons between i and j (inclusive)
  • Use a bottom-up approach to fill the dp table
  • The final answer will be dp[1][n], where n is the number of balloons

Steps to solve by this approach:

 Step 1: Prepare the input by adding 1s at the beginning and end of the array to handle boundary cases.

 Step 2: Create a 2D DP array where dp[i][j] represents the maximum coins obtainable by bursting all balloons from index i to  j.
 Step 3: Use a bottom-up approach, starting with subarrays of length 1 and gradually increasing to the entire array.
 Step 4: For each subarray (i,j), consider each balloon k between i and j as the last one to burst.
 Step 5: When k is the last balloon to burst, the coins obtained are newNums[i-1] * newNums[k] * newNums[j+1].
 Step 6: Add to this the maximum coins from the left subarray (i,k-1) and right subarray (k+1,j).
 Step 7: Return dp[1][n] as the final answer, which represents the maximum coins from the entire array.

Submitted Code :