kt: add «1652. Defuse the Bomb»

URL:	https://leetcode.com/problems/defuse-the-bomb/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-19 22:51:14 +01:00
parent 8a7c5f0a84
commit 700a8d0107
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

36
kt/defuse-the-bomb.kt Normal file
View file

@ -0,0 +1,36 @@
class Solution {
fun decrypt(
code: IntArray,
k: Int,
): IntArray {
val decrypted = IntArray(code.size)
if (k == 0) {
return decrypted
}
var i = 1
if (k < 0) {
i = code.size + k
}
val kAbs = listOf(k, -k).max()
var runningSum = 0
for (j in i..<i + kAbs) {
runningSum += code[j % code.size]
}
for (j in decrypted.indices) {
decrypted[j] = runningSum
runningSum = (
runningSum -
code[i % code.size] +
code[(i + kAbs) % code.size]
)
i += 1
}
return decrypted
}
}