Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 312 Burst Balloons - Walkthrough

Tracing the trick

Pop balloons to find the largest coins. Array [3,1,5,8][3, 1, 5, 8]. Add virtual 1s at ends: [1,3,1,5,8,1][1, 3, 1, 5, 8, 1]. The trick: instead of "which to pop first", think "which to pop last in interval". dp[i][j]dp[i][j] = max coins from popping all balloons in (i,j)(i, j) exclusive, assuming ii and jj are still there.

For interval (0,5)(0, 5) (all real balloons): try each as last. If 33 is last: dp[0][1]+dp[1][5]+131=0++3dp[0][1] + dp[1][5] + 1 \cdot 3 \cdot 1 = 0 + \ldots + 3. Compute recursively.