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: 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
Example 2:
Input: nums = [1,5] Output: 10
Constraints:
n == nums.length1 <= n <= 3000 <= nums[i] <= 100This approach involves using a recursive function to try bursting each balloon and calculating the coins collected, while storing results of already computed subproblems to avoid redundant calculations. We pad the nums array with 1 at both ends to handle boundary conditions easily. For each subproblem defined by indices left and right, we choose a balloon i to burst last and calculate the coins.
This Python solution uses a memoized recursive approach. We define the function dp(left, right) to compute the maximum coins that can be obtained from balloons between left and right. The base case is when there are no balloons to burst between left and right. For each possible balloon i to burst, we maximize our coins by considering bursting i last in that subarray and adding the result from recursive calls to the left and right subproblems.
C++
Java
C#
JavaScript
Time Complexity: O(n^3), where n is the total number of balloons including the padding ones.
Space Complexity: O(n^2) due to memoization table.
In this approach, we use a DP table to systematically build up solutions from smaller problems to a larger one without recursion. We create a dynamic programming matrix dp where dp[left][right] indicates the max coins to be obtained from subarray nums[left:right]. For each subarray defined by len, we consider bursting each possible k as the last balloon and compute the result using previously computed results.
The Python solution uses a bottom-up dynamic programming table to iterate through all subarray lengths from 3 upwards since we initially padded the nums array. This way, we are able to systematically determine the most coins by considering each k element in this sub-array as the last balloon to burst and combining results from smaller subproblems.
C++
Java
C#
JavaScript
Time Complexity: O(n^3)
Space Complexity: O(n^2)
| Approach | Complexity |
|---|---|
| Recursive with Memoization Approach | Time Complexity: O(n^3), where n is the total number of balloons including the padding ones. |
| Dynamic Programming with Bottom-up Approach | Time Complexity: O(n^3) |
DP 51. Burst Balloons | Partition DP | Interactive G-Meet Session Update • take U forward • 168,317 views views
Watch 9 more video solutions →Practice Burst Balloons with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor