Burst Balloons
Implement maxCoins
Given
nums balloons lined up in a row, bursting balloon i earns left · nums[i] · right coins, where left and right are whatever balloons currently sit next to it at the moment it's burst (treat both ends of the row as an invisible balloon of value 1 once nothing real is left there). After a balloon bursts, its former neighbors become adjacent to each other. Burst every balloon, in whichever order earns the most total coins.
The trap is thinking forward — simulating each burst changes the neighbor relationships for every burst after it, so tracking "who's next to whom" gets tangled fast. Thinking about which balloon gets burst *last* inside a shrinking range sidesteps that entirely: whatever's left just outside that range are that balloon's guaranteed final neighbors, since everything else inside the range is already gone by the time it's its turn. That reframes the whole problem as picking, independently for every possible sub-range, which balloon is the last survivor there — exactly the kind of overlapping-subproblem structure interval DP is built for.
Example 1:
Input: nums = [3,1,5,8]
Output: 167
Example 2:
Input: nums = [7]
Output: 7
Example 3:
Input: nums = [1,5]
Output: 10
+ 6 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 300 - ●
0 ≤ nums[i] ≤ 100
nums =
[3, 1, 5, 8]