Delete and Earn

MediumDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums. You repeatedly perform this move: pick any remaining element with value v, earn v points, and remove it. Removing it also destroys **every** remaining element equal to v - 1 or v + 1, which earn nothing. Return the maximum total points you can earn.

Examples

Input: nums = [3,4,2]
Output: 6
Take 4 (destroying 3), then take 2: 4 + 2 = 6.
Input: nums = [2,2,3,3,3,4]
Output: 9
Take all three 3s (the 2s and 4 are destroyed): 9.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems