Skip to content

Commit a48d2a6

Browse files
add 1652
1 parent 3fd5e06 commit a48d2a6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,55 @@ fun numberOfAlternatingGroups(colors: IntArray): Int {
675675
return groups
676676
}
677677

678+
/**
679+
* 1652. Defuse the Bomb
680+
*/
681+
682+
fun decrypt(code: IntArray, k: Int): IntArray = when {
683+
k == 0 -> IntArray(code.size)
684+
k > 0 -> {
685+
val res = IntArray(code.size)
686+
for (i in code.indices) {
687+
var sum = 0
688+
var right = i + 1
689+
var k = k
690+
691+
while (k != 0) {
692+
sum += when {
693+
right >= code.size -> code[right - code.size]
694+
else -> code[right]
695+
}
696+
right++
697+
k--
698+
}
699+
700+
res[i] = sum
701+
}
702+
res
703+
}
704+
705+
else -> {
706+
val res = IntArray(code.size)
707+
for (i in code.indices) {
708+
var sum = 0
709+
var right = i - 1
710+
var k = k
711+
712+
while (k < 0) {
713+
sum += when {
714+
right < 0 -> code[code.size + right]
715+
else -> code[right]
716+
}
717+
right--
718+
k++
719+
}
720+
721+
res[i] = sum
722+
}
723+
res
724+
}
725+
}
726+
678727

679728

680729

0 commit comments

Comments
 (0)