Maximum Points by Deleting Elements
Problem statement
You are given an integer array elements. Repeatedly choose one remaining element with value x and earn x points. The chosen element is removed, and every remaining element equal to x - 1 or x + 1 is deleted.
Continue until no elements remain. Return the maximum total number of points you can earn.
Function
maxPoints(elements: int[]) → longExamples
Example 1
elements = [3,4,2]return = 6Choose 4 to earn 4 points, which deletes the neighboring value 3. Then choose 2 to earn 2 more points. The total is 4 + 2 = 6.
Constraints
1 <= elements.length <= 10^5.1 <= elements[i] <= 10^5for every valid indexi.