Maximum Total Damage With Spell Casting - Problem

๐Ÿง™โ€โ™‚๏ธ A powerful magician has a spellbook filled with various magical spells, each dealing a specific amount of damage. You are given an array power where each element represents the damage value of a spell. Multiple spells can have the same damage value.

However, there's a crucial magical constraint: if a magician casts a spell with damage power[i], the magical interference prevents them from casting any spell with damage values power[i] - 2, power[i] - 1, power[i] + 1, or power[i] + 2.

Each spell can only be cast once, and your goal is to determine the maximum possible total damage the magician can achieve by strategically selecting which spells to cast.

Example: If you have spells with damage [2, 3, 4, 9], you cannot cast both damage-2 and damage-4 spells together (since |2-4| โ‰ค 2), but you can cast damage-2 and damage-9 together.

Input & Output

example_1.py โ€” Basic Case
$ Input: [2, 3, 4, 9]
โ€บ Output: 11
๐Ÿ’ก Note: We can cast spells with damage 2 and 9. We cannot cast damage 3 or 4 together with damage 2 since |2-3|=1 โ‰ค 2 and |2-4|=2 โ‰ค 2. The spell with damage 9 doesn't conflict with damage 2 since |2-9|=7 > 2.
example_2.py โ€” Duplicate Values
$ Input: [1, 1, 6, 9]
โ€บ Output: 17
๐Ÿ’ก Note: We can cast both spells with damage 1 (total contribution: 2), the spell with damage 6 (contribution: 6), and the spell with damage 9 (contribution: 9). None of these conflict: |1-6|=5 > 2, |1-9|=8 > 2, |6-9|=3 > 2. Total: 2 + 6 + 9 = 17.
example_3.py โ€” All Conflict
$ Input: [1, 2, 3]
โ€บ Output: 3
๐Ÿ’ก Note: All spells conflict with each other since |1-2|=1 โ‰ค 2, |1-3|=2 โ‰ค 2, and |2-3|=1 โ‰ค 2. We can only choose one spell, so we choose the one with maximum damage: 3.

Constraints

  • 1 โ‰ค power.length โ‰ค 105
  • 1 โ‰ค power[i] โ‰ค 109
  • Each spell can be cast only once
  • Multiple spells can have the same damage value

Visualization

Tap to expand
๐Ÿง™โ€โ™‚๏ธ Spell Casting Strategy VisualizationExample: [1, 1, 6, 9]1Count: 26Count: 19Count: 1โœ“ OK|6-1|=5>2โœ“ OK|9-6|=3>2DP Calculation:Step 1: dp[0] = 1 ร— 2 = 2 (take both damage-1 spells)dp[0]=2Step 2: dp[1] = max(skip: dp[0], take: dp[0] + 6ร—1) = max(2, 2+6) = 8dp[0]=2+6ร—1=dp[1]=8Step 3: dp[2] = max(skip: dp[1], take: dp[1] + 9ร—1) = max(8, 8+9) = 17dp[1]=8+9ร—1=dp[2]=17๐ŸŽฏ Maximum Total Damage: 17Selected spells: 2 ร— damage-1 + 1 ร— damage-6 + 1 ร— damage-9Conflict RulesโŒ Cannot cast spells with damage d if you cast:โ€ข d-2 (interference range)โ€ข d-1 (close interference)โ€ข d+1 (close interference)โ€ข d+2 (interference range)
Understanding the Visualization
1
Group Similar Spells
Count how many spells you have of each damage type
2
Sort by Power
Arrange spell types in ascending order of damage
3
Dynamic Selection
For each spell type, decide: take all of this type + best from compatible previous types, or skip and take the best from the previous type
Key Takeaway
๐ŸŽฏ Key Insight: Transform the problem into dynamic programming by grouping identical damage values and applying the classic 'non-adjacent selection with gap constraint' pattern on sorted unique values.
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 24
41.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen