Problem Statement

Burst Balloons

You have a row of balloons. Each balloon has a number on it, written in a list called nums, so nums[i] is the number on balloon i. You pop the balloons one at a time. When you pop balloon i, you earn nums[i-1] times nums[i] times nums[i+1] coins, which means the number on the balloon you popped multiplied by the numbers on its left and right neighbors at that moment. If a neighbor is off the edge of the row, treat it as a 1. You pop every balloon. The question is: in what order should you pop them to earn the most coins total? Return that largest possible total.

hardDynamic ProgrammingDynamic ProgrammingTime: O(n^3) · Space: O(n^2)

Signals to notice

divide array into segmentscost depends on what's left after removalthink in reverse

Brute force first

Try every order of balloon bursting. Factorial because each burst changes what's adjacent. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

Interval DP: think of adding balloons instead of removing them. dp[i][j] = max coins from bursting all balloons between i and j (exclusive). For each possible last balloon k in (i,j), dp[i][j] = max(dp[i][k] + dp[k][j] + nums[i]*nums[k]*nums[j]). Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Trace it on nums=[3,1,5,8]

pad -> nums=[1,3,1,5,8,1], n=6, dp all 0; k is the LAST balloon burst in open interval (left,right)
len=2 (adjacent triples): dp[0][2]=1*3*1=3, dp[1][3]=3*1*5=15, dp[2][4]=1*5*8=40, dp[3][5]=5*8*1=40
len=3: dp[0][3]=max(dp[1][3]+1*3*5, dp[0][2]+1*1*5)=max(15+15, 3+5)=30; dp[1][4]=max(40+3*1*8, 15+3*5*8)=max(64,135)=135; dp[2][5]=max(dp[3][5]+1*5*1, dp[2][4]+1*8*1)=max(40+5,40+8)=48
len=4: dp[0][4]=max over k: k=1->dp[1][4]+1*3*8=159, k=2->dp[0][2]+dp[2][4]+1*1*8=3+40+8=51, k=3->dp[0][3]+1*5*8=70 => 159
len=4: dp[1][5]=max: k=2->dp[2][5]+3*1*1=51, k=3->dp[1][3]+dp[3][5]+3*5*1=15+40+15=70, k=4->dp[1][4]+3*8*1=159 => 159
len=5: dp[0][5]=max: k=1->dp[1][5]+1*3*1=162, k=2->dp[0][2]+dp[2][5]+1*1*1=3+48+1=52, k=3->dp[0][3]+dp[3][5]+1*5*1=30+40+5=75, k=4->dp[0][4]+1*8*1=167 => 167
return dp[0][5] = 167

What must stay true

By thinking of k as the LAST balloon burst in the interval (i,j), the left and right subproblems become independent — the boundaries i and j are guaranteed to still exist when k is burst. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Shape of the loop

pad nums with 1 on both ends; dp = n x n grid of 0
for length in 2..n-1:
  for left in 0..n-length-1:
    right = left + length
    for k in left+1..right-1:
      dp[left][right] = max(dp[left][right],
                            dp[left][k] + nums[left]*nums[k]*nums[right] + dp[k][right])
return dp[0][n-1]

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Thinking about which balloon to burst FIRST — that makes subproblems dependent. Reversing the perspective to 'which is burst LAST' is the key insight that makes intervals independent. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Dynamic Programming Pattern