kt: add «2379. Minimum Recolors to Get K Consecutive Black Blocks»

URL:	https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-03-08 11:19:33 +01:00
parent bc6b2af9e7
commit a57370bdd8
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,35 @@
class Solution {
private data class Acc(var l: Int, var white: Int, var recolors: Int) {
constructor() : this(0, 0, Int.MAX_VALUE)
fun update(
blocks: String,
k: Int,
c: IndexedValue<Char>,
): Acc {
if (c.value == 'W') {
white++
}
if (c.index - l + 1 == k) {
recolors = minOf(recolors, white)
if (blocks[l] == 'W') {
white--
}
l++
}
return this
}
}
fun minimumRecolors(
blocks: String,
k: Int,
): Int =
blocks.toCharArray().withIndex().fold(Acc()) { acc, c ->
acc.update(blocks, k, c)
}.recolors
}