Dodomeki
Sign in
← Challenges
‹›

164. The Last Balloon in Each Segment

Hard

A row of balloons is represented by the array nums. Popping a balloon earns points equal to the product of the values of its two neighbors that have not yet been popped. If there is no neighbor on one side, treat that neighbor's value as 1.

Pop every balloon one at a time, choosing an order that maximizes the total points earned. Return that maximum total.

Example 1
Input: nums = [2, 4]
Output: 12
Explanation: Popping the balloon with value 2 first earns 8 points; the other balloon earns 4 more.
Example 2
Input: nums = [1, 2, 3]
Output: 12
Explanation: Popping the middle balloon first earns 6 points, and each of the remaining balloons earns 3.
Example 3
Input: nums = [7]
Output: 7
Explanation: The only balloon has no neighbors, so both are treated as 1.
Constraints
  • 0 <= nums.length <= 300
  • 0 <= nums[i] <= 100
Dynamic programmingArrays
Caso 1
nums =
[2,4]
Expected =
12
Caso 2
nums =
[1,2,3]
Expected =
12
Caso 3
nums =
[7]
Expected =
7

Run checks these 3 cases. Submit runs all 12, including hidden ones.