Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahliu committed Nov 26, 2024
1 parent 232e435 commit 714765d
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/main/kotlin/p32xx/Problem3208.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package p32xx

import util.expect
import kotlin.math.sign

fun main() {
class Solution {
fun numberOfAlternatingGroups(colors: IntArray, k: Int): Int {
var diffCount = (colors.size - k until colors.lastIndex).count {
colors[it] != colors[it + 1]
}
return colors.indices.count { index ->
diffCount += colors[index] xor colors[(index - 1).mod(colors.size)]
diffCount -= colors[(index - k).mod(colors.size)] xor colors[(index - k + 1).mod(colors.size)]

k - 1 == diffCount
}
return (0 until colors.size + k - 1).fold(intArrayOf(0, 0)) { acc, index ->
acc[0] = acc[0] * (colors[index % colors.size] xor colors[(index + 1) % colors.size]) + 1
acc[1] += (acc[0] / k).sign
acc
}[1]
}
}
expect {
Solution().numberOfAlternatingGroups(
intArrayOf(), 1
intArrayOf(0, 1, 0, 1, 0), 3
)
}
}

0 comments on commit 714765d

Please sign in to comment.