Advertisement

Delete and Earn - LeetCode 740 Solution

Delete and Earn - Complete Solution Guide

Delete and Earn is LeetCode problem 740, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . Example 1: Input: nums = [3,4,2] Output: 6 Explanation: You can perform the following oper

Detailed Explanation

The problem asks us to find the maximum points we can earn by deleting numbers from an array. When a number `nums[i]` is deleted, you earn `nums[i]` points, but you must also delete all elements equal to `nums[i] - 1` and `nums[i] + 1`. The input is an array of integers `nums`, and the output is the maximum total points that can be earned following these rules. The constraints are that the array length is between 1 and 20,000, and each number in the array is between 1 and 10,000.

Solution Approach

The solution leverages dynamic programming to solve this problem, drawing an analogy to the House Robber problem. First, it computes the frequency of each number in the input array. Then, based on the frequency, the amount of points that each number can generate is pre-computed (number * frequency). Then a dynamic programming approach is used. At each number, we have two choices: either 'delete and earn' points of this number plus points earned up to two numbers before (as deleting the current number precludes deleting its neighbors), or don't delete the current number and just keep the maximum points earned until the previous number. This is efficiently solved using a space-optimized DP approach with just two variables to track the previous and current maximum points.

Step-by-Step Algorithm

  1. Step 1: Create a frequency map (or array) to store the counts of each number in the input `nums`.
  2. Step 2: Determine the maximum number in the input `nums`. This is needed to initialize an array to hold the total points for each number.
  3. Step 3: Create an array `points` where `points[i]` stores the points earned by taking all instances of number `i`. This is equal to `i * count[i]`.
  4. Step 4: Initialize two variables, `prev` and `curr`, to 0. These will represent the maximum points earned up to `i-2` and `i-1` respectively, during the dynamic programming stage.
  5. Step 5: Iterate through the `points` array from the smallest to the largest number. At each index `i`, calculate the maximum points that can be earned by either taking the points at index `i` plus the points earned up to index `i-2` (`prev + points[i]`), or by not taking the points at index `i` and carrying forward the points earned up to index `i-1` (`curr`). Update `prev` and `curr` accordingly. Specifically, the new `prev` is the old `curr`, and the new `curr` is the maximum of old `curr` and `prev + points[i]`.
  6. Step 6: After iterating through the entire `points` array, the variable `curr` will hold the maximum points that can be earned. Return `curr`.

Key Insights

  • Insight 1: The problem can be transformed into a variation of the House Robber problem. This is because choosing a number prevents you from choosing its immediate neighbors (n-1 and n+1).
  • Insight 2: Use a frequency map to represent the points available for each number. This allows us to represent each number's worth (number * frequency) effectively.
  • Insight 3: Dynamic programming can be efficiently applied using a bottom-up approach to determine the maximum points attainable at each number based on whether to include or exclude it.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Dynamic Programming.

Companies

Asked at: Accenture, Akuna Capital, Citadel.