Skip to main content

Delete and Earn - Solution & Explanation

MediumArrayHash TableDynamic Programming8 min readAsked at: Amazon, Microsoft, Meta +9
Practice this problem

Problem Statement

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
Explanation: You can perform the following operations:
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
- Delete 2 to earn 2 points. nums = [].
You earn a total of 6 points.

Example 2:

Input: nums = [2,2,3,3,3,4]
Output: 9
Explanation: You can perform the following operations:
- Delete a 3 to earn 3 points. All 2's and 4's are also deleted. nums = [3,3].
- Delete a 3 again to earn 3 points. nums = [3].
- Delete a 3 once more to earn 3 points. nums = [].
You earn a total of 9 points.

 

Constraints:

  • 1 <= nums.length <= 2 * 104
  • 1 <= nums[i] <= 104

Approach Overview

Problem Overview: You are given an array of integers. Picking a number x earns x points, but forces you to delete every occurrence of x-1 and x+1. The goal is to maximize total points. The key realization: picking values affects neighboring values, which makes this problem structurally identical to the classic House Robber dynamic programming pattern.

Approach 1: Dynamic Programming with Map (O(n log n) time, O(n) space)

Start by aggregating the total points each number can contribute. Use a hash map where the key is the number and the value is the total points earned from all occurrences of that number. After building the map, extract and sort the unique keys. Then run dynamic programming across the sorted values. If two numbers are consecutive (difference of 1), choosing one prevents choosing the other, just like adjacent houses in House Robber. Maintain two states: the best score including the current number and excluding it. Sorting costs O(n log n), while the DP pass is linear. This approach works well when the value range is large but the number of unique elements is relatively small. It relies heavily on hash table lookups and ordered traversal.

Approach 2: Dynamic Programming with Points Array (O(n + maxVal) time, O(maxVal) space)

This version removes sorting entirely by converting the problem into a dense array. First compute the maximum value in the input. Create a points array where points[i] stores the total points earned from value i. Iterate through the input and accumulate points[num] += num. Once built, the array behaves exactly like the House Robber problem: at index i, either take points[i] plus the best result from i-2, or skip it and keep the result from i-1. This produces a simple linear DP transition. The algorithm runs in O(n + maxVal) time with O(maxVal) space. Because it converts the problem into a sequential DP scan, it cleanly demonstrates patterns used in dynamic programming and array-based state transitions.

Recommended for interviews: The points-array dynamic programming approach is typically expected. It clearly shows that you recognized the hidden House Robber pattern and reduced the problem to a one‑dimensional DP recurrence. Mentioning the map-based approach first demonstrates problem exploration, but the array DP solution highlights stronger algorithmic insight and achieves linear performance without sorting.

Approach 1: Dynamic Programming with Points Array

This approach is based on converting the problem into another known dynamic programming problem, similar to the 'House Robber' problem. The idea is to first calculate the total points for each number by multiplying its value by its frequency in the array. This allows us to transform the problem into finding the optimal way to select values in a linear array where selecting one value prevents selecting its direct neighbors.

This Python solution starts by identifying the maximum number in the array to establish the range of a new points array. It increments each position in the points array by the value times its occurrence. We then iterate over this points array, applying a dynamic programming strategy similar to solving 'House Robber', determining for each value whether taking it or skipping it yields more points.

Code

Python

C++

Complexity

Time Complexity: O(n + m) where n is the number of elements in nums and m is the maximum number in nums.
Space Complexity: O(m) used by the points array.

Try this approach in the editor →

Approach 2: Dynamic Programming with Map

This approach uses a map to store the points for each number, avoiding the space usage related to the maximum number in the points array and processing only numbers that exist in the input. We can still utilize a dynamic programming strategy to compute maximum points.

In this Java solution, we map each number to its accumulated points. This avoids predefined array size based on max number and allows compact representation for sparse input. We then iterate over the map and use dynamic programming to find the maximum points.

Code

Java

JavaScript

Complexity

Time Complexity: O(n + k) where n is the number of elements and k is the number of unique elements in nums.
Space Complexity: O(k) for storing elements in map.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Dynamic Programming with Points Array

Time Complexity: O(n + m) where n is the number of elements in nums and m is the maximum number in nums.
Space Complexity: O(m) used by the points array.

Dynamic Programming with Map

Time Complexity: O(n + k) where n is the number of elements and k is the number of unique elements in nums.
Space Complexity: O(k) for storing elements in map.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming with MapO(n log n)O(n)When values are sparse or the maximum value is very large compared to array size
Dynamic Programming with Points ArrayO(n + maxVal)O(maxVal)General case and most interview settings where value range is manageable

Video Solution

Delete and Earn - Dynamic Programming - Leetcode 740 - Python • NeetCode • 65,407 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Delete and Earn easy or hard?
Delete and Earn is rated Medium on LeetCode because the main challenge is recognizing the hidden transformation to the House Robber dynamic programming problem. Once that pattern is identified, the implementation becomes a straightforward linear DP.
How to solve Delete and Earn in O(n)?
Aggregate the total score contributed by each number into a points array where points[i] = i multiplied by its frequency. Then apply the House Robber recurrence: dp[i] = max(dp[i-1], dp[i-2] + points[i]). This avoids sorting and processes the values sequentially.
What is the best approach for Delete and Earn?
The best approach converts the problem into the House Robber dynamic programming pattern. First aggregate total points for each value, then run DP where choosing value i prevents choosing i-1 and i+1. Using a points array allows a linear scan and runs in O(n + maxVal) time with O(maxVal) space.
Is Delete and Earn asked at Google/Amazon/Meta?
Delete and Earn appears frequently in technical interview preparation lists and has been reported in interviews at companies like Amazon and Meta. The problem tests recognition of dynamic programming patterns, particularly transforming problems into the House Robber recurrence.
What data structure is used in Delete and Earn?
Common implementations use either a hash map or an array to aggregate the total points for each value. After aggregation, dynamic programming is applied across the values to decide whether to include or skip each number.
What is the time complexity of Delete and Earn?
The optimal solution runs in O(n + maxVal) time using a points array and dynamic programming. Building the points array takes O(n), and the DP scan across values up to the maximum number takes O(maxVal). A map-based version with sorted keys runs in O(n log n).
Delete and Earn Python or Java solution approach?
Both Python and Java implementations typically follow the same two steps: accumulate points for each value and then apply House Robber style DP. Python often uses a list for the points array, while Java implementations frequently use either arrays or a HashMap followed by sorted traversal.

Ready to solve this problem?

Practice Delete and Earn with our built-in code editor and test cases.

Practice on FleetCode