Skip to content

Commit 51c9f76

Browse files
committed
Improved task
1 parent c35da55 commit 51c9f76

File tree

1 file changed

+11
-7
lines changed
  • src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting

1 file changed

+11
-7
lines changed

src/main/kotlin/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g3101_3200.s3186_maximum_total_damage_with_spell_casting
22

33
// #Medium #Array #Hash_Table #Dynamic_Programming #Sorting #Binary_Search #Two_Pointers #Counting
4-
// #2024_06_21_Time_51_ms_(99.29%)_Space_60.8_MB_(78.34%)
4+
// #2024_06_22_Time_1106_ms_(92.73%)_Space_81.1_MB_(41.82%)
55

66
import kotlin.math.max
77
import kotlin.math.min
@@ -24,9 +24,10 @@ class Solution {
2424
}
2525
val dp = LongArray(maxPower + 6)
2626
dp[1] = counts[1].toLong()
27-
dp[2] = max((counts[2] * 2L), dp[1])
27+
dp[2] = max((counts[2] * 2L).toDouble(), dp[1].toDouble()).toLong()
2828
for (i in 3..maxPower) {
29-
dp[i] = max((counts[i] * i + dp[i - 3]), max(dp[i - 1], dp[i - 2]))
29+
dp[i] = max((counts[i] * i + dp[i - 3]).toDouble(), max(dp[i - 1].toDouble(), dp[i - 2].toDouble()))
30+
.toLong()
3031
}
3132
return dp[maxPower]
3233
}
@@ -44,12 +45,15 @@ class Solution {
4445
count++
4546
} else {
4647
val curVal = max(
47-
(curPower * count + prevs[3]),
48-
max(prevs[1], prevs[2])
48+
(curPower.toLong() * count + prevs[3]).toDouble(),
49+
max(prevs[1].toDouble(), prevs[2].toDouble())
4950
)
50-
val diff = min((p - curPower), (prevs.size - 1))
51+
.toLong()
52+
val diff = min((p - curPower).toDouble(), (prevs.size - 1).toDouble()).toInt()
5153
val nextCurVal =
52-
if ((diff == 1)) 0 else max(prevs[3], max(curVal, prevs[2]))
54+
if ((diff == 1)) 0 else max(prevs[3].toDouble(), max(curVal.toDouble(), prevs[2].toDouble()))
55+
.toLong()
56+
// Shift the values in prevs[].
5357
var k = prevs.size - 1
5458
if (diff < prevs.size - 1) {
5559
while (k > diff) {

0 commit comments

Comments
 (0)