Burst Balloons - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,1,5,8]
Output: 167
💡 Note: One optimal order: burst balloon 1 (coins = 3*1*5 = 15), then burst balloon 5 (coins = 3*5*8 = 120), then balloon 3 (coins = 1*3*8 = 24), finally balloon 8 (coins = 1*8*1 = 8). Total = 15 + 120 + 24 + 8 = 167
Example 2 — Single Balloon
$ Input: nums = [1,5]
Output: 10
💡 Note: Burst balloon 1 first (coins = 1*1*5 = 5), then balloon 5 (coins = 1*5*1 = 5). Total = 5 + 5 = 10
Example 3 — Minimum Size
$ Input: nums = [3]
Output: 3
💡 Note: Only one balloon to burst: coins = 1*3*1 = 3

Constraints

  • 1 ≤ nums.length ≤ 500
  • 0 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Burst Balloons: Maximize Coins315811Coins = left × current × rightExample: Burst balloon with value 1Coins = 3 × 1 × 5 = 15Remaining balloons: [3, 5, 8]Maximum Coins: 167🎯 Key: Try all possible orders!
Understanding the Visualization
1
Input
Array of balloons with numbers [3,1,5,8]
2
Process
Burst balloons optimally to maximize coins
3
Output
Maximum coins possible: 167
Key Takeaway
🎯 Key Insight: Think backwards - decide which balloon to burst LAST in each range, not first
Asked in
Google 42 Facebook 38 Amazon 29 Apple 15
312.0K Views
Medium Frequency
~35 min Avg. Time
8.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen