Delete and Earn

Given an array nums, you can choose a value x and earn x points for each occurrence of x, but then you must delete every occurrence of x - 1 and x + 1. Return the maximum points you can earn. This is a choice DP problem that reduces to house-robber style selection over values.

Input Format

nums = array of positive integers

Output Format

maximum points you can earn

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 delete 3 and 5; total points = 4 + 2 = 6.

Example 2:

Input:

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

Output:

9

Explanation:

Take 3 three times for 9 points.

Loading...
Delete and Earn - Dp Knapsack DSA Problem