Tutorialspoint
Problem
Solution
Submissions

Burst Balloons

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C# program to implement the MaxCoins(int[] nums) function. 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.

Example 1
  • Input: nums = [3, 1, 5, 8]
  • Output: 167
  • Explanation:
    • Step 1: Consider all possible orders of bursting the balloons.
    • Step 2: For the optimal order:
      • Burst balloon with value 1: 3 * 1 * 5 = 15 coins
      • Remaining balloons: [3, 5, 8]
      • Burst balloon with value 5: 3 * 5 * 8 = 120 coins
      • Remaining balloons: [3, 8]
      • Burst balloon with value 3: 1 * 3 * 8 = 24 coins
      • Remaining balloons: [8]
      • Burst balloon with value 8: 1 * 8 * 1 = 8 coins
    • Step 3: Calculate the total coins: 15 + 120 + 24 + 8 = 167
    • Step 4: Return the maximum possible coins, which is 167.
Example 2
  • Input: nums = [1, 5]
  • Output: 10
  • Explanation:
    • Step 1: Consider all possible orders of bursting the balloons.
    • Step 2: For the optimal order:
      • Burst balloon with value 1: 1 * 1 * 5 = 5 coins
      • Remaining balloons: [5]
      • Burst balloon with value 5: 1 * 5 * 1 = 5 coins
    • Step 3: Calculate the total coins: 5 + 5 = 10
    • Step 4: Return the maximum possible coins, which is 10.
Constraints
  • n == nums.length
  • 1 ≤ n ≤ 500
  • 0 ≤ nums[i] ≤ 100
  • Time Complexity: O(n³) where n is the number of balloons
  • Space Complexity: O(n²) for the dynamic programming table
ArraysDynamic Programming IBMeBay
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 with a bottom-up approach
  • Consider the problem from a different angle: instead of thinking about which balloon to burst first, think about which one to burst last
  • Create a 2D array to store the maximum coins you can get by bursting all balloons between indices i and j
  • For each possible range, try each balloon as the last one to burst
  • Compute the value recursively and fill the DP table

Steps to solve by this approach:

 Step 1: Create a new array with 1s at both ends to handle boundary cases.
 Step 2: Initialize a 2D dp array where dp[i][j] represents the maximum coins obtained from bursting all balloons from index i to j.
 Step 3: Iterate through all possible subarray lengths.
 Step 4: For each subarray, try each balloon as the last one to burst.
 Step 5: When choosing the last balloon k to burst, add the coins from bursting k (left-1 * k * right+1) plus the maximum coins from the left and right subarrays.
 Step 6: Fill the dp table bottom-up, starting with smaller subarrays.
 Step 7: Return dp[1][n], which represents the maximum coins from bursting all original balloons.

Submitted Code :