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 you choose. 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 that are still in the row. A missing neighbour, past either end of the row, counts as 1. After a burst, the 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 first for 2 × 4 × 3 = 24 coins. The 2 and the 3 are now neighbours, so bursting the 2 earns 1 × 2 × 3 = 6, and the 3, now alone, earns 1 × 3 × 1 = 3. That makes 33, and no other order does better: bursting the small 2 first already caps you at 24.

lock icon+15 hidden tests on Submit

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 cases

Case 1

Case 2

Case 3

Input

nums = [2, 4, 3]

Expected

33