You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons. If you burst the i-th balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it. Return the maximum coins you can collect by bursting the balloons wisely.
Example 1
Input: nums = [3,1,5,8]
Output: 167
Explanation: Burst the balloons in the order 1, 5, 3, 8. The array goes [3,1,5,8] → [3,5,8] → [3,8] → [8] → [], collecting 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 15 + 120 + 24 + 8 = 167 coins.
Example 2
Input: nums = [1,5]
Output: 10
Explanation: Burst 1 first to earn 1*1*5 = 5 (the left neighbour is an imaginary 1), then burst 5 for 1*5*1 = 5. The total is 10. Bursting 5 first would only give 5 + 1 = 6.
Example 3
Input: nums = [7]
Output: 7
Explanation: The only balloon has imaginary 1s on both sides, so bursting it gives 1*7*1 = 7.
Instead of deciding which balloon to burst first, decide which one is burst LAST in each open interval. When k is last, its neighbours are the fixed interval boundaries, so the two sub-intervals become independent.
Run checks all cases above. Submit evaluates all test cases.