
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
Editorial
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. |
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