Community Problem

Delete and Earn

Difficulty: Medium

Maximize total points earned by picking values, where picking value v deletes v-1 and v+1, by reducing to House Robber on a frequency line.

Delete and Earn

Maximize total points earned by picking values, where picking value v deletes v-1 and v+1, by reducing to House Robber on a frequency line.

MEDIUM
Free
dynamic-programming
arrays
hash-table

By CodeSnatch

December 19, 2025

·

Updated May 20, 2026

262 views

4

4.5 (9)

I had this on a Snowflake systems-onsite and the moment I sketched a frequency array, my interviewer just said "yes, that's House Robber." The reduction is the whole problem: aggregate by VALUE (not by index), then run House Robber on the value-line where each cell v carries weight v * count[v]. The catalog covered house-robber, but it skipped this counting variant where the input is unsorted and values repeat.

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. After this, 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.

Examples

Example 1:

  • Input: nums = [3, 4, 2]
  • Output: 6
  • Explanation: Pick 4 (earn 4, delete the 3 and 5s; the 2 stays), then pick 2 (earn 2). Total 4 + 2 = 6.

Example 2:

  • Input: nums = [2, 2, 3, 3, 3, 4]
  • Output: 9
  • Explanation: Pick all three 3s (earn 9; the 2s and 4s are deleted as side effects). 9 > 2 * 2 + 4 = 8.

Example 3:

  • Input: nums = [1]
  • Output: 1
  • Explanation: Pick the only element.

Example 4:

  • Input: nums = [1, 1, 1, 2, 4, 5, 5, 5, 6]
  • Output: 18
  • Explanation: Pick all 1s (3) skip 2, then choose 5s (15). Total 3 + 15 = 18.

Constraints

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

Follow-up

Why does aggregating by value reduce this to House Robber? Once you decide to take ANY copy of value v, you might as well take ALL copies (the deletion side effects are the same). So the decision is per-value, not per-index. The constraint "taking v forbids taking v - 1 and v + 1" is exactly the no-adjacent-houses rule of House Robber on a frequency-weighted line indexed by value.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems