Maximum Points by Deleting Elements
Problem statement
You are given an integer array elements. Repeat these operations until the array is empty:
- Select a remaining value
v. - Remove every occurrence of
vand add their sum to your score. - Remove every occurrence of
v - 1andv + 1without scoring those values.
Return the maximum score that can be earned.
Function
maxPoints(elements: int[]) → longExamples
Example 1
elements = [5,6,6,4,11]return = 27Take 11 for 11 points, both copies of 6 for 12 points, and 4 for 4 points, totaling 27.
Example 2
elements = [3,4,2]return = 6Choose 2 and 4. They do not conflict and contribute 2 + 4 = 6.
Constraints
1 <= elements.length <= 10^51 <= elements[i] <= 10^5