Delete and Earn (LeetCode 740): Solution and Explanation
Picking a number earns its value but wipes out every copy of the numbers one above and one below it. Find the most points you can collect.
Intuition
Once you take one copy of a value v, all copies of v - 1 and v + 1 are gone, so nothing stops you from taking the remaining copies of v too. Each distinct value is therefore all or nothing, worth v times its count.
After grouping, the only rule left is that you cannot take two values that differ by exactly one. Lay the distinct values out in sorted order and that is House Robber: neighbouring houses cannot both be robbed.
The twist is gaps. If the value before v is not v - 1 (say 3 then 5), taking both is fine, so v can build on the best total so far instead of only on the skip total.
Approach: Buckets, then take or skip in value order
- Sum points per value:
total[v] = v * count(v). - Walk the distinct values in ascending order with two numbers:
take(best total if this value is taken) andskip(best total if it is not). - Let
best = max(take, skip)from the previous value. - If
v == prev + 1, the previous value conflicts, sotake = skip + total[v]. Otherwisetake = best + total[v]. - Set
skip = best,prev = v, and finally returnmax(take, skip).
Step-by-step example
nums = [3,4,2]
- Buckets: 2 is worth 2, 3 is worth 3, 4 is worth 4.
- Value 2 is the first bucket: take = 2, skip = 0.
- Value 3 is next to 2: take = skip(2) + 3 = 3, skip = max(2, 0) = 2.
- Value 4 is next to 3: take = skip(3) + 4 = 2 + 4 = 6, skip = max(3, 2) = 3.
- The answer is max(6, 3) = 6: take 2 and 4, and 3 burns.
Code: Python, Java, C++
1class Solution:
2 def deleteAndEarn(self, nums: List[int]) -> int:
3 total = {}
4 for x in nums:
5 total[x] = total.get(x, 0) + x
6 take, skip = 0, 0
7 prev = -1
8 for v in sorted(total):
9 best = max(take, skip)
10 if v == prev + 1:
11 take = skip + total[v]
12 else:
13 take = best + total[v]
14 skip = best
15 prev = v
16 return max(take, skip)1class Solution {
2 public int deleteAndEarn(int[] nums) {
3 TreeMap<Integer, Integer> total = new TreeMap<>();
4 for (int x : nums) {
5 total.merge(x, x, Integer::sum);
6 }
7 int take = 0, skip = 0;
8 int prev = -1;
9 for (int v : total.keySet()) {
10 int best = Math.max(take, skip);
11 if (v == prev + 1) {
12 take = skip + total.get(v);
13 } else {
14 take = best + total.get(v);
15 }
16 skip = best;
17 prev = v;
18 }
19 return Math.max(take, skip);
20 }
21}1class Solution {
2public:
3 int deleteAndEarn(vector<int>& nums) {
4 map<int, int> total;
5 for (int x : nums) {
6 total[x] += x;
7 }
8 int take = 0, skip = 0;
9 int prev = -1;
10 for (const auto& kv : total) {
11 int v = kv.first, pts = kv.second;
12 int best = max(take, skip);
13 if (v == prev + 1) {
14 take = skip + pts;
15 } else {
16 take = best + pts;
17 }
18 skip = best;
19 prev = v;
20 }
21 return max(take, skip);
22 }
23};Time and space complexity
O(n + k log k)
Bucketing is one pass over nums; sorting the k distinct values costs k log k, and the take/skip walk is linear in k.
O(k)
The map from value to points has one entry per distinct value; the DP itself is two numbers.
Other approaches
Array of size max + 1 (time O(n + max), space O(max))
Make an array points[0..max] and run House Robber over every integer up to max, including the empty ones. No sorting is needed, but the work depends on the largest value instead of the number of distinct values.
Recursion over choices (time O(2^k), space O(k))
Try every subset of distinct values that has no two consecutive numbers. Correct but exponential.
Common mistakes
- Treating copies separately. Taking one 3 does not burn the other 3s, so all copies of a value should be taken together.
- Using the House Robber rule across a gap. If the previous value is v - 2 or smaller, there is no conflict and v can add to the best total.
- Iterating in input order instead of sorted value order. Adjacency is about values, not positions in nums.
- Overflow worries in the wrong place: the total is at most 2 * 10^4 * 10^4 = 2 * 10^8, which fits in a 32-bit int.
Frequently asked questions
Why is this the same as House Robber?
After grouping, each distinct value is a house worth v * count, and taking value v forbids its numeric neighbours v - 1 and v + 1, just like robbing a house forbids the houses next to it.
Why is it safe to take every copy of a value?
The first copy you take already deletes all v - 1 and v + 1 elements. Taking more copies of v deletes nothing new, so it only adds points.
What changes when values have gaps?
Nothing conflicts across a gap, so the current value can be added to the best total seen so far rather than to the total that skipped the previous value.