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:
parent
bc6b2af9e7
commit
a57370bdd8
1 changed files with 35 additions and 0 deletions
35
kt/minimum-recolors-to-get-k-consecutive-black-blocks.kt
Normal file
35
kt/minimum-recolors-to-get-k-consecutive-black-blocks.kt
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue