Skip to main content

Burst Balloons

HardPremium

You have n balloons in a row, and balloon i has the number nums[i] painted on it. When you burst balloon i, you earn left * nums[i] * right coins, where left and right are the numbers on the balloons currently next to it. Once a balloon bursts, its two neighbors become next to each other.

If there's no balloon on one side, treat that side as a balloon with the number 1. Burst every balloon, in whatever order you like, and return the most coins you can earn.

Example 1

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

One best order: burst 1 for 3 * 1 * 5 = 15, then 5 for 3 * 5 * 8 = 120, then 3 for 1 * 3 * 8 = 24, then 8 for 1 * 8 * 1 = 8. The total is 167.

Example 2

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

Bursting 1 first earns 1 * 1 * 5 = 5, then 5 earns 1 * 5 * 1 = 5.

Constraints

  • 1 <= nums.length <= 300
  • 0 <= nums[i] <= 100