Delete and Earn Variant

Given an array nums, choose numbers to earn points equal to their values times frequency. Choosing x removes x-1 and x+1 from consideration. Pattern focus: Take Or Skip DP. After compressing equal values, the problem reduces to a take-or-skip decision over sorted unique numbers.

Input Format

nums = array of integers

Output Format

maximum points obtainable

Constraints

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

Examples

Example 1:

Input:

nums = [3,4,2]

Output:

6

Explanation:

Take 4 and 2.

Example 2:

Input:

nums = [2,2,3,3,3,4]

Output:

9

Explanation:

Taking 3 yields the best total.

Loading...
Delete and Earn Variant - Dp 1d DSA Problem