FastPrepMaximum Points by Deleting Elements

Maximum Points by Deleting Elements

Walmart logoWalmart● MediumFULLTIMEOA
Learn

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[]) → long

Examples

Example 1

elements = [3,4,2]return = 6

Choose 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^5 for every valid index i.

More Walmart problems

See Walmart hiring insights
public long maxPoints(int[] elements) {
    // write your code here
}
elements[3,4,2]
expected6
Checking account…