Leetcode 740: Delete and Earn

grid47
grid47
Exploring patterns and algorithms
Aug 25, 2024 5 min read

A series of numbers where the optimal strategy to delete and earn is highlighted, glowing softly as the calculation is made
Solution to LeetCode 740: Delete and Earn Problem

You are given an integer array nums. You want to maximize the number of points you can earn 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.
Problem
Approach
Steps
Complexity
Input: The input is an integer array representing the numbers from which you will be selecting elements to delete and earn points.
Example: nums = [5, 6, 4]
Constraints:
• 1 <= nums.length <= 2 * 10^4
• 1 <= nums[i] <= 10^4
Output: Return the maximum number of points you can earn by performing the operation some number of times.
Example: For nums = [5, 6, 4], the output should be 8.
Constraints:
Goal: Maximize the total points earned from deleting elements and ensuring no adjacent elements (nums[i] - 1 or nums[i] + 1) are left.
Steps:
• Count the occurrences of each number in the array.
• Create a dynamic programming array where dp[i] stores the maximum points obtainable by considering numbers from 1 to i.
• For each number, either choose to delete it and earn points or skip it and carry forward the previous maximum.
• Return the final value from the dynamic programming array.
Goal: The input will always satisfy the given constraints, ensuring a manageable number of operations.
Steps:
• 1 <= nums.length <= 2 * 10^4
• 1 <= nums[i] <= 10^4
Assumptions:
• The input array nums will always contain at least one element.
Input: For nums = [5, 6, 4], the output is 8.
Explanation: By deleting the number 6 and 4, we maximize our points by choosing the best elements to remove.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus