740. Delete and Earn
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to maximize points by deleting numbers from an array. When we delete a number x to earn x points, we are forced to also delete all numbers equal to x-1 and x+1. A key observation is that if we decide to take a number x, we should take all occurrences of x. This is because taking the first x already imposes the penalty of deleting all x-1's and x+1's. Taking subsequent x's gives us more points without any additional penalty.
Solution
Delete and Earn in Python
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
if not nums:
return 0
# Find the maximum value to determine the size of our points array.
max_val = max(nums)
# Create an array to store the total points for each number.
# points[i] = sum of all numbers equal to i.
points = [0] * (max_val + 1)
for num in nums:
points[num] += num
# Apply the House Robber logic with space optimization.
# We only need to keep track of the last two calculated max points.
prev2 = 0 # Represents the max points from robbing up to i-2
prev1 = 0 # Represents the max points from robbing up to i-1
# Iterate from 1 up to max_val
for i in range(1, max_val + 1):
# Current max is either not robbing house i (prev1) or robbing house i (prev2 + points[i])
current_max = max(prev1, prev2 + points[i])
prev2 = prev1
prev1 = current_max
return prev1
Delete and Earn in Java
class Solution {
public int deleteAndEarn(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
// Find the maximum value in nums to determine the size of our points array.
int maxVal = 0;
for (int num : nums) {
maxVal = Math.max(maxVal, num);
}
// Create an array to store the total points for each number.
// points[i] = sum of all numbers equal to i.
int[] points = new int[maxVal + 1];
for (int num : nums) {
points[num] += num;
}
// Apply the House Robber logic with space optimization.
// We only need to keep track of the last two calculated max points.
int prev2 = 0; // Represents the max points from robbing up to i-2
int prev1 = 0; // Represents the max points from robbing up to i-1
// Handle the base case for points[1]
if (maxVal >= 1) {
prev1 = points[1];
}
// Iterate from 2 up to maxVal
for (int i = 2; i <= maxVal; i++) {
// Current max is either not robbing house i (prev1) or robbing house i (prev2 + points[i])
int current_max = Math.max(prev1, prev2 + points[i]);
prev2 = prev1;
prev1 = current_max;
}
return prev1;
}
}
Delete and Earn in C++
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
if (nums.empty()) {
return 0;
}
// Find the maximum value in nums to determine the size of our points array.
int maxVal = 0;
for (int num : nums) {
maxVal = max(maxVal, num);
}
// Create an array to store the total points for each number.
// points[i] = sum of all numbers equal to i.
vector<int> points(maxVal + 1, 0);
for (int num : nums) {
points[num] += num;
}
// Apply the House Robber logic with space optimization.
// We only need to keep track of the last two calculated max points.
int prev2 = 0; // Represents the max points from robbing up to i-2
int prev1 = 0; // Represents the max points from robbing up to i-1
// Handle the base case for points[1]
if (maxVal >= 1) {
prev1 = points[1];
}
// Iterate from 2 up to maxVal
for (int i = 2; i <= maxVal; ++i) {
// Current max is either not robbing house i (prev1) or robbing house i (prev2 + points[i])
int current_max = max(prev1, prev2 + points[i]);
prev2 = prev1;
prev1 = current_max;
}
return prev1;
}
};
Delete and Earn in TypeScript
function deleteAndEarn(nums: number[]): number {
if (nums.length === 0) {
return 0;
}
// Find the maximum value in nums to determine the size of our points array.
let maxVal = 0;
for (const num of nums) {
maxVal = Math.max(maxVal, num);
}
// Create an array to store the total points for each number.
// points[i] = sum of all numbers equal to i.
const points = new Array(maxVal + 1).fill(0);
for (const num of nums) {
points[num] += num;
}
// Apply the House Robber logic with space optimization.
// We only need to keep track of the last two calculated max points.
let prev2 = 0; // Represents the max points from robbing up to i-2
let prev1 = 0; // Represents the max points from robbing up to i-1
// Handle the base case for points[1]
if (maxVal >= 1) {
prev1 = points[1];
}
// Iterate from 2 up to maxVal
for (let i = 2; i <= maxVal; i++) {
// Current max is either not robbing house i (prev1) or robbing house i (prev2 + points[i])
const current_max = Math.max(prev1, prev2 + points[i]);
prev2 = prev1;
prev1 = current_max;
}
return prev1;
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.