AlgoFrames

Burst balloons

Standard · Write code · about 20 min

Balloons stand in a row with the values in nums. Bursting balloon i pays left · nums[i] · right coins, where left and right are the values of its current neighbours. A missing neighbour at either end counts as 1. After a burst, the two neighbours become adjacent.

Burst every balloon and return the most coins you can collect.

Example 1

Input:

[[3,1,5,8]]

Output:

167

Burst 1, 5, 3, 8: 15 + 120 + 24 + 8 = 167.

Example 2

Input:

[[1,5]]

Output:

10

Burst 1, then 5: 5 + 5 = 10.

Constraints

  • 1 ≤ nums.length ≤ 300
  • 0 ≤ nums[i] ≤ 100

Learn the idea first: Interval DP: matrix chain, burst balloons, palindrome partitioning, a visual lesson.

More Interval DP: matrix chain, burst balloons, palindrome partitioning problems