/*
* @lc app=leetcode.cn id=2379 lang=typescript
*
* [2379] 得到 K 个黑块的最少涂色次数
*/
// @lc code=start
function minimumRecolors(blocks: string, k: number): number {}
// @lc code=end
function minimumRecolors(blocks: string, k: number): number {
let res = Infinity,
cnt = 0
for (let i = 0; i < blocks.length; i++) {
if (i >= k) {
res = Math.min(res, cnt)
if (blocks[i - k] === 'W') cnt--
}
if (blocks[i] === 'W') cnt++
}
return Math.min(res, cnt)
}
test.each([
{ input: { blocks: 'WBBWWBBWBW', k: 7 }, output: 3 },
{ input: { blocks: 'WBWBBBW', k: 2 }, output: 0 },
])('input: blocks = $input.blocks, k = $input.k', ({ input: { blocks, k }, output }) => {
expect(minimumRecolors(blocks, k)).toEqual(output)
})