
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²)
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 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