HardArrayDynamic Programming

Burst Balloons

LeetCode
1 approach, code in all languages

You are given n balloons in a row, each painted with a number stored in nums. You will burst all of them, one balloon per step.

When you burst the balloon at index i, you collect nums[left] * nums[i] * nums[right] coins, where left and right are the indices of the balloons still remaining directly beside i. If a neighbor no longer exists because it is off the ends of the row, treat its value as 1 (imagine an implicit balloon of value 1 padding both boundaries).

Return the maximum number of coins you can collect after every balloon has been burst.

Example 1

Input: nums = [3,1,5,8]

Output: 167

Bursting in the order 1, 5, 3, 8 yields 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 15 + 120 + 24 + 8 = 167.

Example 2

Input: nums = [1,5]

Output: 10

Burst 1 first for 1*1*5 = 5, then burst 5 for 1*5*1 = 5, totaling 10.

Constraints

  • n == nums.length
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now