Menu
CoddyTech

Burst Balloons

A row of balloons is given as nums, where nums[i] is the number on balloon i. You burst all of them, one at a time, in any order. Bursting a balloon earns left × nums[i] × right coins, where left and right are the numbers on its current neighbours, the nearest balloons on each side still in the row. A missing neighbour counts as 1. After a burst, its two neighbours become adjacent. Return the most coins you can collect.

Function

maxCoins(nums: integer-array) → integer
numsinteger-array
the numbers on the balloons, from left to right
Returnsinteger
the most coins you can collect by bursting every balloon

Constraints

  • 1 ≤ nums.length ≤ 300
  • 0 ≤ nums[i] ≤ 100
  • The answer is below 3 × 108, so it fits in a 32-bit signed integer.

Examples

Input
nums = [2, 4, 3]
Output
33
Explanation
Burst the 4 for 2 × 4 × 3 = 24. The 2, now next to the 3, earns 1 × 2 × 3 = 6, and the 3 earns 3: 33 in all.
challenge icon

Follow-up

Can you also return one bursting order that earns the most coins?

Reset code
def maxCoins(nums):
    # Write code here

Test #1

Test #2

Test #3

Argument

nums = [2, 4, 3]

Expected Output

33