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:
parent
8a7c5f0a84
commit
700a8d0107
1 changed files with 36 additions and 0 deletions
36
kt/defuse-the-bomb.kt
Normal file
36
kt/defuse-the-bomb.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue