Skip to main content

Burst Balloons

Burst all balloons to get maximum coins Bursting balloon i gives nums[i-1] * nums[i] * nums[i+1] coins Out of bounds treated as 1 Example: [3,1,5,8] => 167 [3,1,5,8] -> [3,5,8] -> [3,8] -> [8] -> [] coins: 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167

You are given an array nums where nums[i] is the number on balloon i. Bursting balloon i earns nums[left] * nums[i] * nums[right] coins, where left and right are the balloons currently next to it, and a missing neighbor at either end counts as 1. Once a balloon bursts, its two neighbors become adjacent. Burst every balloon and return the maximum total coins you can collect; for example, [3,1,5,8] yields 167. nums is non-empty and every value is between 0 and 100.

Examples

You write

max_coins(nums: List[int])

  1. Example 1

    Input
    nums = [3,1,5,8]
    returns
    167
  2. Example 2

    Input
    nums = [1,5]
    returns
    10

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Dynamic Programming

I reach for it when

  • Brute force repeats the same subproblem.
  • You are asked for a count, a minimum or a maximum.

Say this before you type. Say the state in one sentence, then say which smaller states it reads from.

All 37 Dynamic Programming problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Maximum Coins from Bursting Balloonshard