Delete and Earn
Problem Statement
You have a list of whole numbers. You may repeat this move as many times as you like: pick any number x in the list, delete it, and earn x points. Then you must also delete every number equal to x − 1 and every number equal to x + 1.
Copies of x are not deleted by the move, so you can pick them one by one. Return the most points you can earn.
nums, a list of whole numbers. Repeats are allowed.
The largest number of points you can earn.
Pick 4 to earn 4. That deletes the 3 (4 − 1). The 2 is still there, so pick it to earn 2. Total 6.
Pick a 3 three times. Each pick earns 3, so that is 9. The first pick deletes the 2s and 4s, but the other 3s stay, so all three can be picked.
Values 1 and 5 are not next to each other, so both can be used: 1 + 1 + 5 = 7.
- • 1 ≤ nums.length ≤ 2 × 10^4
- • 1 ≤ nums[i] ≤ 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Group the values and recurse over them in order: take or skip, and no taking v right after v − 1
Sort and group, then run a dp; a value clashes with the previous one only when it is exactly one more
Store points[v] = v × count(v) in an array indexed by value, then run House Robber over it
Group the numbers by value, so each distinct value v is worth v × count. Walk the distinct values from smallest to largest. For each one, either take it, which is blocked when the value just below it was taken, or skip it. Keep the larger total.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — 1D Linear translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.