Tutorialspoint
Problem
Solution
Submissions

Burst Balloons

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

Write a C 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 i-th 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 at index 1 (value 1): coins = 3*1*5 = 15, remaining = [3,5,8].
    • Burst balloon at index 1 (value 5): coins = 3*5*8 = 120, remaining = [3,8].
    • Burst balloon at index 0 (value 3): coins = 1*3*8 = 24, remaining = [8].
    • Burst balloon at index 0 (value 8): coins = 1*8*1 = 8, remaining = [].
    • Total coins = 15 + 120 + 24 + 8 = 167.
Example 2
  • Input: nums = [1,5]
  • Output: 10
  • Explanation:
    • Burst balloon at index 0 (value 1): coins = 1*1*5 = 5, remaining = [5].
    • Burst balloon at index 0 (value 5): coins = 1*5*1 = 5, remaining = [].
    • Total coins = 5 + 5 = 10.
Constraints
  • n == nums.length
  • 1 ≤ n ≤ 300
  • 0 ≤ nums[i] ≤ 100
  • Time Complexity: O(n^3)
  • Space Complexity: O(n^2)
Dynamic Programming AirbnbTutorix
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 interval-based approach
  • Add boundary balloons with value 1 at both ends
  • For each interval [left, right], try bursting each balloon as the last one
  • When bursting balloon k last in interval [left, right], multiply values[left] * values[k] * values[right]
  • Use memoization to store results for overlapping subproblems
  • Build solution bottom-up from smaller intervals to larger ones

Steps to solve by this approach:

 Step 1: Add boundary balloons with value 1 at both ends of the array
 Step 2: Create a 2D memoization table to store results of subproblems
 Step 3: Use interval DP where dp[left][right] represents max coins from interval (left, right)
 Step 4: For each interval, try bursting each balloon as the last one in that interval
 Step 5: When balloon k is burst last, coins = values[left] * values[k] * values[right]
 Step 6: Add coins from left and right subintervals recursively
 Step 7: Use memoization to avoid recalculating same subproblems

Submitted Code :