-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3186-maximum-total-damage-with-spell-casting.js
More file actions
55 lines (46 loc) · 1.55 KB
/
3186-maximum-total-damage-with-spell-casting.js
File metadata and controls
55 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 3186. Maximum Total Damage With Spell Casting
* https://leetcode.com/problems/maximum-total-damage-with-spell-casting/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} power
* @return {number}
*/
var maximumTotalDamage = function(power) {
const frequency = new Map();
for (const p of power) {
frequency.set(p, (frequency.get(p) || 0) + 1);
}
const uniquePowers = Array.from(frequency.keys()).sort((a, b) => a - b);
const n = uniquePowers.length;
if (n === 0) return 0;
if (n === 1) return uniquePowers[0] * frequency.get(uniquePowers[0]);
const dp = new Array(n).fill(0);
dp[0] = uniquePowers[0] * frequency.get(uniquePowers[0]);
for (let i = 1; i < n; i++) {
const currentPower = uniquePowers[i];
const currentDamage = currentPower * frequency.get(currentPower);
let j = i - 1;
while (j >= 0 && uniquePowers[j] >= currentPower - 2) {
j--;
}
const withCurrent = currentDamage + (j >= 0 ? dp[j] : 0);
const withoutCurrent = dp[i - 1];
dp[i] = Math.max(withCurrent, withoutCurrent);
}
return dp[n - 1];
};