Delete And Earn Solution for Accenture

Company: Accenture · Difficulty: Medium · Topics: Array, Dynamic Programming, Hash Table

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.

Key Understanding:

Examples

Example 1:

Input: nums = [3,4,2]

Output: 6

Explanation:

Alternative approach:

Example 2:

Input: nums = [2,2,3,3,3,4]

Output: 9

Explanation:

Note: After deleting 3, all 2's and 4's are automatically deleted, so we can't earn additional points from them.

Example 3:

Input: nums = [1,1,1,2,4,5,5,5,6]

Output: 18

Explanation:

Intuition

This problem is similar to the "House Robber" problem. The key insight is that if we choose to take a number, we cannot take its adjacent numbers (number-1 and number+1).

Why This Works:

Dynamic Programming Approach:

Approach

Approach 1: Dynamic Programming with Full Array

Algorithm:

1. Count frequency of each number in the array

2. Create a frequency array where freq[i] = count of number i

3. Use DP: dp[i] = max(dp[i-1], dp[i-2] + freq[i] * i)

4. Return dp[max_number]

Why This Works:

Approach 2: Optimized DP with Two Variables

Algorithm:

1. Count frequency of each number

2. Use two variables (prev2, prev1) to track previous states

3. Iterate through all possible numbers (1 to max_number)

4. Update maximum points using the same DP formula

Approach 3: Recursive with Memoization

Algorithm:

1. Count frequency of each number

2. Use recursive function with memoization

3. For each number, try taking it or skipping it

4. Return the maximum of both choices

Dry Run

Let's trace through Example 1: nums = [3,4,2]

Step-by-Step Process:

1. Count frequencies:

2. DP Array:

3. Result: dp[4] = 6

Optimized Approach:

Key Insights

1. House Robber Analogy: This is essentially House Robber with frequency counts

2. Adjacent Constraint: Taking a number prevents taking adjacent numbers

3. Frequency Multiplication: Total value = frequency × number

4. Optimal Substructure: Solution for range [1...i] depends on [1...i-1] and [1...i-2]

5. Greedy Won't Work: Always taking the largest number doesn't guarantee optimal solution

Edge Cases

1. Empty Array: Return 0

2. Single Element: Return the value of that element

3. All Same Numbers: Return count × number

4. Consecutive Numbers: Only one can be chosen

5. Large Numbers: Handle efficiently with optimized DP

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Optimized Dynamic Programming (Best)

Time Complexity: O(n + M) where n is array length and M is max number

Space Complexity: O(1) - Constant extra space

Solution 2: Full DP Array

Time Complexity: O(n + M)

Space Complexity: O(M) - DP array size

Solution 3: Recursive with Memoization

Time Complexity: O(n + M)

Space Complexity: O(M) - Memoization array

More Accenture coding questions