Skip to content

Commit 9a64214

Browse files
committed
[LeetCode Sync] Runtime - 481 ms (67.06%), Memory - 39.8 MB (89.10%)
1 parent 925dfc7 commit 9a64214

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>A magician has various spells.</p>
2+
3+
<p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p>
4+
5+
<p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</strong> cast any spell with a damage of <code>power[i] - 2</code>, <code>power[i] - 1</code>, <code>power[i] + 1</code>, or <code>power[i] + 2</code>.</p>
6+
7+
<p>Each spell can be cast <strong>only once</strong>.</p>
8+
9+
<p>Return the <strong>maximum</strong> possible <em>total damage</em> that a magician can cast.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<div class="example-block">
15+
<p><strong>Input:</strong> <span class="example-io">power = [1,1,3,4]</span></p>
16+
17+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
18+
19+
<p><strong>Explanation:</strong></p>
20+
21+
<p>The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.</p>
22+
</div>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">power = [7,1,6,6]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">13</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.</p>
34+
</div>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= power.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>1 &lt;= power[i] &lt;= 10<sup>9</sup></code></li>
42+
</ul>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def maximumTotalDamage(self, power):
3+
damageFrequency = defaultdict(int)
4+
for damage in power:
5+
damageFrequency[damage] += 1
6+
7+
uniqueDamages = sorted(damageFrequency.keys())
8+
totalUniqueDamages = len(uniqueDamages)
9+
maxDamageDP = [0] * totalUniqueDamages
10+
11+
maxDamageDP[0] = uniqueDamages[0] * damageFrequency[uniqueDamages[0]]
12+
13+
for i in range(1, totalUniqueDamages):
14+
currentDamageValue = uniqueDamages[i]
15+
currentDamageTotal = currentDamageValue * damageFrequency[currentDamageValue]
16+
17+
maxDamageDP[i] = maxDamageDP[i - 1]
18+
19+
previousIndex = i - 1
20+
while (previousIndex >= 0 and
21+
(uniqueDamages[previousIndex] == currentDamageValue - 1 or
22+
uniqueDamages[previousIndex] == currentDamageValue - 2 or
23+
uniqueDamages[previousIndex] == currentDamageValue + 1 or
24+
uniqueDamages[previousIndex] == currentDamageValue + 2)):
25+
previousIndex -= 1
26+
27+
if previousIndex >= 0:
28+
maxDamageDP[i] = max(maxDamageDP[i], maxDamageDP[previousIndex] + currentDamageTotal)
29+
else:
30+
maxDamageDP[i] = max(maxDamageDP[i], currentDamageTotal)
31+
32+
return maxDamageDP[totalUniqueDamages - 1]
33+
34+

0 commit comments

Comments
 (0)