Problem Description
Given an array power, where each element represents the damage of a spell, determine the maximum total damage that can be achieved by casting a subset of these spells under the restriction that if you cast a spell with damage d, you cannot cast any spell with damage d-2, d-1, d+1, or d+2. Each spell can only be cast once.
Key Insights
- The problem is similar to "Delete and Earn" but with an extended conflict range (neighbors are d-2, d-1, d+1, and d+2).
- Aggregate spells with the same damage value; for each unique damage, calculate totalDamage = damage * frequency.
- Sort the unique damage values to consider conflicts in a sequential order.
- Use dynamic programming: for each unique damage value, decide whether to take it (and add the accumulated damage from non-conflicting previous spells) or skip it.
- When the current damage and the previous damage differ by at most 2, they conflict; otherwise, the current damage can be safely added without conflicts.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting the unique damage values, where n is the length of the power array. Space Complexity: O(n) for storing the frequency dictionary and the dp array.
Solution
We start by counting the frequency of each damage value and multiply it by the damage itself to get the total damage contribution for that damage value. Sorting the keys allows us to process damage values in order and detect conflicts by checking if the difference between consecutive keys is less than or equal to 2. Using dynamic programming, for each unique damage d:
- If d conflicts with the previous damage value (i.e., current damage - previous damage <= 2), update the dp state by taking the maximum of skipping the current damage or adding it to dp[i-2] (if available).
- If there is no conflict (the gap is at least 3), safely add the current total damage to the previous dp state. The final dp state gives the maximum total damage.