#3186
Maximum Total Damage With Spell Casting
medium · 45.1% accepted · 750 likes · top 28%
array · hash table · two pointers · binary search · dynamic programming · sorting · counting
Description
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.
Solution