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.
Example 1:
Input: power = [1,1,3,4]
Output: 6
Explanation:
The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.
Example 2:
Input: power = [7,1,6,6]
Output: 13
Explanation:
The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.
Constraints:
1 <= power.length <= 1051 <= power[i] <= 109This approach is inspired by the house robber problem where you cannot rob two consecutive houses for maximum profit.
Sort the array to ensure sequential consideration of spells and use a dynamic programming array to keep track of the maximum damage possible after considering each spell.
The state transition will involve checking if taking the current spell is beneficial over not taking it, given the constraint that certain nearby spells cannot be taken if the current one is taken.
The approach involves sorting the power array, then using a dynamic programming approach to decide whether to include the current spell damage or not. It relies on previous results and skips adjacent spells that violate constraints. The final result is stored in the 'current' variable.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting followed by O(n) dynamic programming iteration.
Space Complexity: O(1) since we only use a constant amount of extra space for variables.
The greedy approach first sorts the spells to manage constraints easily.
Loop over sorted array to select spells ensuring the constraint condition by skipping spells adjacent to any selected spell's damage value.
Directly keep a set of selected spells to avoid overlapping selections.
Sort the array of spell damages, then iterate through selecting any spell that doesn't violate constraints determined by the previously included spell damage. The loop accumulates allowable spells' damage directly into the result.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting plus O(n) for the iteration.
Space Complexity: O(1) with constant variables used for computations.
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n log n) due to sorting followed by O(n) dynamic programming iteration. |
| Greedy Approach with Sorting | Time Complexity: O(n log n) due to sorting plus O(n) for the iteration. |
3186. Maximum Total Damage With Spell Casting | DP + Binary Search | Same as LIS • Aryan Mittal • 5,333 views views
Watch 9 more video solutions →Practice Maximum Total Damage With Spell Casting with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor