> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Burst Balloons Python Solution with Tests

> Tested Python solution for LeetCode 312 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 312, Hard. Topics: Array, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/burst-balloons/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 312   # by problem number
lcpy gen -s burst_balloons   # by problem name
```

## 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*.

### Examples

```
Input: nums = [3,1,5,8]
Output: 167
```

**Explanation:**
nums = \[3,1,5,8] --> \[3,5,8] --> \[3,8] --> \[8] --> \[]
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167

```
Input: nums = [1,5]
Output: 10
```

### Constraints

* n == nums.length
* 1 \<= n \<= 300
* 0 \<= nums\[i] \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/burst_balloons/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/burst_balloons/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^3)
    # Space: O(n^2)
    def max_coins(self, nums: list[int]) -> int:
        balloons = [1, *nums, 1]
        n = len(balloons)
        # dp[i][j] = max coins bursting all balloons strictly between i and j
        dp = [[0] * n for _ in range(n)]
        for length in range(2, n):
            for i in range(n - length):
                j = i + length
                for k in range(i + 1, j):
                    coins = balloons[i] * balloons[k] * balloons[j]
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + coins)
        return dp[0][n - 1]
```

## Complexity

| Time   | Space  |
| ------ | ------ |
| O(n^3) | O(n^2) |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
