Delete and Earn

You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:

  • Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1.

Return the maximum number of points you can earn by applying the above operation some number of times.

Example 1
Inputnums = [3,4,2]
Output6
Deleting 4 earns 4 points and removes 3, then deleting 2 earns 2 more points for a total of 6.
Example 2
Inputnums = [2,2,3,3,3,4]
Output9
Deleting all three 3s earns 9 total points, while all 2s and 4 are removed after the first 3 is deleted.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • 1 <= nums[i] <= 10^4

Asked at 11 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate