Burst Balloons

Hard

There are n balloons in a row, and balloon i has the number nums[i] painted on it. You burst them one at a time until none are left.

Bursting a balloon earns left × middle × right coins, where middle is the number on that balloon and left and right are the numbers on the balloons currently next to it. If there is no balloon on one side, use 1 for that side.

Once a balloon bursts, the balloons on either side of it become neighbors. Return the maximum number of coins you can collect.

Examples

Example 1

Input:  nums = [3,1,5,8]
Output: 167
Example 1: nums = [3,1,5,8]1315813 × 1 × 5 = 15135813 × 5 × 8 = 12013811 × 3 × 8 = 241811 × 8 × 1 = 8Total: 167Filled: the balloon that burstsDashed: no balloon there, counts as 1

The total is 15 + 120 + 24 + 8 = 167.

Example 2

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

Burst 1 first for 1 × 1 × 5 = 5, then 5 for 1 × 5 × 1 = 5. Bursting 5 first only earns 5 + 1 = 6.

Example 3

Input:  nums = [7]
Output: 7

There are no neighbors on either side, so the only burst earns 1 × 7 × 1 = 7.

Example 4

Input:  nums = [0,5,0]
Output: 5

Burst both 0s first. They earn nothing, but then 5 is alone and earns 1 × 5 × 1 = 5. Bursting 5 while the 0s are still next to it earns 0 × 5 × 0 = 0.

Example 5

Input:  nums = [2,4,8]
Output: 88

Burst 4 first for 2 × 4 × 8 = 64, then 2 for 1 × 2 × 8 = 16, then 8 for 1 × 8 × 1 = 8. 64 + 16 + 8 = 88.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
TypeScript
Loading editor…
Test cases31
Case 1
nums = [3,1,5,8]
Case 2
nums = [1,5]
Case 3
nums = [7]
Case 4
nums = [0,5,0]
Case 5
nums = [2,4,8]
Case 6
nums = [0]
Case 7
nums = [100]
Case 8
nums = [0,0,0]
Case 9
nums = [1,1,1,1]
Case 10
nums = [100,100]
Case 11
nums = [5,10]
Case 12
nums = [9,76,64,21]
Case 13
nums = [8,2,6,8,9,8,1,4,1,5,3,0,7,7,0,4,2,2,5]
Case 14
nums = [100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100]
Case 15
nums = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Case 16
nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Case 17
nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
Case 18
nums = [100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
Case 19
nums = [50,0,100,100,0,1,0,1,100,0,0,1,100,1,1,0,100,50,50,1,50,0,0,100,1,50,100,1,1,50,1,1,50,0,50,100,0,0,100,100,0,100,0,100,100,0,0,50,1,0,1,1,0,0,50,0,1,0,1,1,0,0,0,1,100,0,50,1,1,100,1,1,100,50,1,100,0,50,0,100,1,50,0,1,50,1,100,0,50,0,0,1,50,0,1,0,100,1,100,0]
Case 20
nums = [0,78]
Case 21
nums = [4,72,5]
Case 22
nums = [5,66,85,1,2]
Case 23
nums = [43]
Case 24
nums = [12,4,32]
Case 25
nums = [2,4,15,8]
Case 26
nums = [30,21,76,27,35,64,11,69,96,66,85,64,85,63,16,45,41,45,73,25,4,39,90,75,74,44,21,78,23,97,76,83,57,75,97,6,62,21,19,72,27,8,91,73,99,95,24,63,9,10,9,69,87,92,85,43,59,17,78,82,75,92,78,34,39,21,34,51,99,23,67,45,98,22,74,2,2,97,54,49,49,1,42,93,30,30,33,95,29,48,21,10,10,71,20,46,39,18,36,57]
Case 27
nums = [97,24,2,62,34,78,100,84,62,95,25,55,33,43,33,37,69,41,88,8,14,68,51,0,25,49,83,23,12,25,55,32,39,6,3,59,87,96,25,11,83,72,77,94,86,67,39,53,81,12,86,14,85,62,80,24,18,10,43,58,23,94,82,69,34,46,60,43,71,14,80,42,28,80,22,87,96,29,43,1,48,95,68,64,16,97,61,6,4,55,58,48,85,25,28,50,72,83,25]
Case 28
nums = [65,85,24,67,98,87,79,80,41,89,64,78,53,21,32,83,96,14,49,22,84,21,81,72,31,42,90,63,86,31,65,49,6,28,64,78,39,75,91,83,9,52,94,65,29,72,42,31,95,65,47,44,31,24,51,87,89,82,13,42,57,53,78,3,93,67,72,74,74,60,50,30,74,56,42,32,44,62,25,42]
Case 29
nums = [72,51,14,37,82,72,25,76,12,15,68,32,35,81,95,91,66,40,31,89,43,58,8,92,67,91,55,80,87,42,54,11,99,40,22,5,14,61,74,53,80,14,11,25,58,60,36,46,54,57,60,69,72,27,58,55,51,47,67,25,48,64,96,10]
Case 30
nums = [25,79,95,23,8,99,100,84,72,89,77,62,50,62,49,72,68,34,44,48,23,91,59,22,4,38,10,0,57,90,33,94,42,90,13,66,36,24,44,84]
Case 31
nums = [24,46,93,48,95,12,75,36,98,86,40,0,38,72,54,19,62,1,63,67,6,59,77,18,38]