Algorithm


Problem Name: 740. Delete and Earn

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

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        int n = nums.size();
        if(n == 0) return 0;
        sort(nums.begin(), nums.end());
        vector<vector < int>>dp(n, vector<int>(2));
        dp[0][0] = 0;
        dp[0][1] = nums[0];
        for(int i = 1; i  <  n; i++){
            if(nums[i] == nums[i - 1]){
                dp[i][0] = dp[i - 1][0];
                dp[i][1] = dp[i - 1][1] + nums[i];
                continue;
            }
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
            dp[i][1] = nums[i] == nums[i - 1] + 1 ? dp[i - 1][0] + nums[i] : dp[i][0] + nums[i];
        }
        return max(dp[n - 1][0], dp[n - 1][1]>;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,4,2]

Output

x
+
cmd
6

#2 Code Example with Javascript Programming

Code - Javascript Programming


const deleteAndEarn = function (nums) {
  const n = 10001
  const values = new Array(n).fill(0)
  for (let num of nums) values[num] += num

  let take = 0,
    skip = 0
  for (let i = 0; i  <  n; i++) {
    const takei = skip + values[i]
    const skipi = Math.max(skip, take)
    take = takei
    skip = skipi
  }
  return Math.max(take, skip)
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,4,2]

Output

x
+
cmd
6

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def deleteAndEarn(self, nums):
        cnt, dp, maxs = collections.Counter(nums), {}, {}
        nums = sorted(set(nums))
        if len(nums) < 2:
            return nums and nums[0] * cnt[nums[0]] or 0
        for i in range(len(nums)):
            dp[i] = nums[i] * cnt[nums[i]]
            if i >= 2:
                if nums[i - 1] < nums[i] - 1:
                    dp[i] += maxs[i - 1]
                else:
                    dp[i] += maxs[i - 2]
                maxs[i] = max(dp[i], maxs[i - 1])
            elif i:
                if nums[i - 1] < nums[i] - 1:
                    dp[i] += dp[i - 1]
                maxs[i] = max(dp[i], dp[i - 1])
            else:
                maxs[i] = dp[i]
        return max(dp[len(nums) - 1], dp[len(nums) - 2])
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,2,3,3,3,4]

Output

x
+
cmd
9
Advertisements

Demonstration


Previous
#739 Leetcode Daily Temperatures Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#741 Leetcode Cherry Pickup Solution in C, C++, Java, JavaScript, Python, C# Leetcode