-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 7
More file actions
26 lines (18 loc) · 685 Bytes
/
Day 7
File metadata and controls
26 lines (18 loc) · 685 Bytes
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
#3186.maximum-total-damage-with-spell-casting
from collections import Counter
class Solution:
def maximumTotalDamage(self, power):
damage_sum = Counter(power)
unique = sorted(damage_sum.keys())
n = len(unique)
dp = [0] * n
for i in range(n):
take = damage_sum[unique[i]] * unique[i]
# Find last non-conflicting index
j = i - 1
while j >= 0 and unique[i] - unique[j] <= 2:
j -= 1
if j >= 0:
take += dp[j]
dp[i] = max(dp[i-1] if i > 0 else 0, take)
return dp[-1]