Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 312 Burst Balloons - Transition

Last balloon

For dp[i][j]dp[i][j], try each balloon kk in (i,j)(i,j) as the last one to burst. Coins = dp[i][k]dp[i][k] + dp[k][j]dp[k][j] + nums[i]×nums[k]×nums[j]nums[i] \times nums[k] \times nums[j].

The first two terms are coins from clearing the left and right. The last term is from bursting k when only i and j remain as neighbors. dp[i][j]dp[i][j] = max over all kk in (i,j)(i,j) of this expression. The formula tells you how to compute dp[i][j] from smaller ranges. Getting this right is the core of DP.