Maximum Total Damage With Spell Casting - Problem
A magician has various spells. You are given an array power, where each element represents the damage of a spell. Multiple spells can have the same damage value.
It is a known fact that if a magician decides to cast a spell with a damage of power[i], they cannot cast any spell with a damage of power[i] - 2, power[i] - 1, power[i] + 1, or power[i] + 2.
Each spell can be cast only once. Return the maximum possible total damage that a magician can cast.
Input & Output
Example 1 — Basic Case
$
Input:
power = [1,1,3,4]
›
Output:
6
💡 Note:
We can cast both spells with damage 1 and the spell with damage 4. The spell with damage 4 conflicts with damage 2,3,5,6 (which don't exist or can't be cast), so we can cast damage 1 twice and damage 4 once. Total = 1 + 1 + 4 = 6. We cannot include damage 3 because it conflicts with damage 1 and 4.
Example 2 — Multiple Same Values
$
Input:
power = [7,1,6,6]
›
Output:
13
💡 Note:
We can cast both spells with damage 6 and the spell with damage 1. Total = 6 + 6 + 1 = 13. We cannot include damage 7 because it conflicts with damage 6.
Example 3 — All Consecutive
$
Input:
power = [1,2,3,4,5]
›
Output:
5
💡 Note:
All damages are consecutive and conflict with each other. The best strategy is to pick the single highest damage spell: 5.
Constraints
- 1 ≤ power.length ≤ 105
- 1 ≤ power[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code