-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.py
More file actions
20 lines (20 loc) · 767 Bytes
/
solution.py
File metadata and controls
20 lines (20 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def maximumTotalDamage(self, power: List[int]) -> int:
from collections import Counter
if not power:
return 0
cnt = Counter(power)
vals = sorted(cnt.keys()) # sorted unique damage values
m = len(vals)
value_sum = [vals[i] * cnt[vals[i]] for i in range(m)]
dp = [0] * m
dp[0] = value_sum[0]
import bisect
for i in range(1, m):
need = vals[i] - 3 # last allowed value <= need
# find rightmost index <= need
j = bisect.bisect_right(vals, need, 0, i) - 1
take = value_sum[i] + (dp[j] if j >= 0 else 0)
skip = dp[i - 1]
dp[i] = max(skip, take)
return dp[-1]