Skip to content
Decoder

Delete and Earn

Medium

Problem

You are given an array nums. In one move you pick any nums[i], earn nums[i] points, and remove it. Every element equal to `nums[i] - 1` or `nums[i] + 1` is then deleted too, for no points. Return the maximum points you can earn.

Example 1

Input
nums = [3, 4, 2]
Output
6
Why
take 4, which deletes the 3; then take 2

Example 2

Input
nums = [2, 2, 3, 3, 3, 4]
Output
9
Why
take all three 3s

Example 3

Input
nums = [1]
Output
1

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.