↓Skip to main content
  1. LeetCode/

LeetCode 312: Burst Balloons

·2 mins· ·
LeetCode Hard Dynamic-Programming Interval-Dp Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-06-26

  • Submissions: Runtime Error → Accepted ×2

Approach
#

Choosing the first balloon to burst makes its two sides neighbors afterwards, so the subproblems stop being independent.

Choose the last one instead: pad both ends with 1, val = [1] + nums + [1], and let dp[i][j] = the best score for bursting every balloon in the open range (i, j). If k is the last one burst in (i, j):

  • When k bursts, (i, k) and (k, j) are already empty, so its neighbors are i and j, scoring val[i] × val[k] × val[j].
  • While (i, k) and (k, j) are being burst, k is still there, so the two sides do not affect each other.
dp[i][j] = max over k in (i, j) of dp[i][k] + dp[k][j] + val[i] × val[k] × val[j]

The answer is dp[0][n + 1].

My Attempts
#

Attempt 1 misspelled a variable: it declared max_coin but the loop wrote max_coins = max(max_coins, coins), raising UnboundLocalError. Making the names match passed.

Solution
#

class Solution:
    def maxCoins(self, nums: List[int]) -> int:
        val = [1] + nums + [1]
        n = len(val)
        dp = [[0] * n for _ in range(n)]           # dp[i][j]: open range (i, j)
        for length in range(2, n):
            for i in range(n - length):
                j = i + length
                dp[i][j] = max(dp[i][k] + dp[k][j] + val[i] * val[k] * val[j]
                               for k in range(i + 1, j))
        return dp[0][n - 1]

My accepted version was a memoized recursion dfs(i, j) that took 3.5 seconds; the bottom-up loop by length is somewhat faster.

Complexity
#

Time O(n^3), space O(n^2).

Mistakes to Avoid
#

  • Choosing the first balloon burst: the subproblems are not independent.
  • Using closed ranges: neighbors get awkward; open ranges with 1s padded at the ends are cleanest.

Interview Explanation
#

I choose the last balloon burst in a range, because by then both sides are empty, its neighbors are the range’s ends, and the two sides are independent. With 1s padded at the ends, dp[i][j] is the best for the open range: left plus right plus val[i] × val[k] × val[j]. Filling by increasing length gives O(n^3).

Related

LeetCode 1537: Get the Maximum Score
·3 mins
LeetCode Hard Dynamic-Programming Two-Pointers Greedy Array
LeetCode 1537 note: two sorted arrays can switch only at shared values; merge with two pointers, keep a running sum per path, and at a shared value set both to the larger sum plus that value
LeetCode 1458: Max Dot Product of Two Subsequences
·2 mins
LeetCode Hard Dynamic-Programming Longest-Common-Subsequence Array
LeetCode 1458 note: a two-prefix DP; dp[i][j] either pairs nums1[i] with nums2[j] (extending dp[i-1][j-1] or starting fresh) or skips one of them
LeetCode 42: Trapping Rain Water
·2 mins
LeetCode Hard Array Two-Pointers Dynamic-Programming Stack
LeetCode 42 note: each cell holds min(tallest on the left, tallest on the right) - its own height; use prefix and suffix maxima or two pointers
LeetCode 3651: Minimum Cost Path with Teleportations
·2 mins
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Array
LeetCode 3651 note: compute layer by layer over teleports; arriving at a cell by teleport costs the previous layer’s minimum over cells with value >= it, found with a running minimum after sorting, followed by the usual right / down grid DP
LeetCode 84: Largest Rectangle in Histogram
·3 mins
LeetCode Daily Hard Array Stack Monotonic-Stack Dynamic-Programming Geometry Histogram
Solving the Largest Rectangle in Histogram problem using monotonic stack approach
LeetCode 3906: Count Good Integers on a Grid Path
·2 mins
LeetCode Hard Dynamic-Programming Digit-Dp
LeetCode 3906 note: the range reaches 9 × 10^15, so no per-number checks; a digit DP carries both lower and upper tight flags, plus the previous digit on the path since path cells must be non-decreasing