Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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 * 1041 <= nums[i] <= 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Dynamic Programming
[3,4,2]
[2,2,3,3,3,4]
house-robber)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #740: Delete and Earn
class Solution {
public int deleteAndEarn(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] sums = new int[10010];
int[] select = new int[10010];
int[] nonSelect = new int[10010];
int maxV = 0;
for (int x : nums) {
sums[x] += x;
maxV = Math.max(maxV, x);
}
for (int i = 1; i <= maxV; i++) {
select[i] = nonSelect[i - 1] + sums[i];
nonSelect[i] = Math.max(select[i - 1], nonSelect[i - 1]);
}
return Math.max(select[maxV], nonSelect[maxV]);
}
}
// Accepted solution for LeetCode #740: Delete and Earn
func deleteAndEarn(nums []int) int {
max := func(x, y int) int {
if x > y {
return x
}
return y
}
mx := math.MinInt32
for _, num := range nums {
mx = max(mx, num)
}
total := make([]int, mx+1)
for _, num := range nums {
total[num] += num
}
first := total[0]
second := max(total[0], total[1])
for i := 2; i <= mx; i++ {
cur := max(first+total[i], second)
first = second
second = cur
}
return second
}
# Accepted solution for LeetCode #740: Delete and Earn
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
mx = -inf
for num in nums:
mx = max(mx, num)
total = [0] * (mx + 1)
for num in nums:
total[num] += num
first = total[0]
second = max(total[0], total[1])
for i in range(2, mx + 1):
cur = max(first + total[i], second)
first = second
second = cur
return second
// Accepted solution for LeetCode #740: Delete and Earn
struct Solution;
impl Solution {
fn delete_and_earn(nums: Vec<i32>) -> i32 {
let n = 10001;
let mut sum: Vec<i32> = vec![0; n];
let mut dp: Vec<i32> = vec![0; n];
for x in nums {
sum[x as usize] += x;
}
dp[1] = sum[1];
for i in 2..n {
dp[i] = i32::max(sum[i] + dp[i - 2], dp[i - 1]);
}
dp[n - 1]
}
}
#[test]
fn test() {
let nums = vec![3, 4, 2];
let res = 6;
assert_eq!(Solution::delete_and_earn(nums), res);
let nums = vec![2, 2, 3, 3, 3, 4];
let res = 9;
assert_eq!(Solution::delete_and_earn(nums), res);
}
// Accepted solution for LeetCode #740: Delete and Earn
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #740: Delete and Earn
// class Solution {
// public int deleteAndEarn(int[] nums) {
// if (nums.length == 0) {
// return 0;
// }
//
// int[] sums = new int[10010];
// int[] select = new int[10010];
// int[] nonSelect = new int[10010];
//
// int maxV = 0;
// for (int x : nums) {
// sums[x] += x;
// maxV = Math.max(maxV, x);
// }
//
// for (int i = 1; i <= maxV; i++) {
// select[i] = nonSelect[i - 1] + sums[i];
// nonSelect[i] = Math.max(select[i - 1], nonSelect[i - 1]);
// }
// return Math.max(select[maxV], nonSelect[maxV]);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.