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.

30:00

Delete and Earn
medium
Free written solution
Topics
Companies

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:
Input: {"nums":[3,4,2]}
Output: 6
Constraints:
  • 1≤nums.length≤2×1041 \leq \text{nums.length} \leq 2 \times 10^4

  • 1≤nums[i]≤1041 \leq \text{nums}[i] \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,4,2]}

Drop nums[0] = 3 into bucket 3. Taking 3 means taking every copy, so the bucket is worth 3.

nums

3
4
2

take

skip

2

-
-

.

3

3

-
-

.

4

-
-

.

A dashed line marks a missing value: buckets on either side of it never burn each other.

Variables
VariableValue
x3
total[3]3
DepthFunction Call
Stack empty
0/7