Skip to content
BytePatterns

Pop Balloons For Coins

HardDynamic Programming#interval-dp#bottom-up-dp~50m

Problem

A row of balloons each carries a number. Popping a balloon pays its number multiplied by the numbers of the balloons currently to its left and right; a missing neighbour counts as 1. The row closes up after every pop, so the neighbours change as you go. Pop every balloon and return the largest total payment possible.

Examples

Input:  values = [3, 1, 5, 8]
Output: 167
Why:    popping in the order 1, 5, 3, 8 pays 15 + 120 + 24 + 8
Input:  values = [1, 5]
Output: 10
Why:    popping the 1 first pays 5, and the lone 5 then pays another 5
Input:  values = []
Output: 0
Why:    edge case, there is nothing to pop

Hints

0 / 3

Stuck on the idea rather than the code? Interval DP covers it.