#2379

Minimum Recolors to Get K Consecutive Black Blocks

newbie · 295 · lc easy +20 · verified · 68.6% accepted · 1,315 likes · top 76%

Description

You are given a 0-indexed string blocks of length n consisting of 'W' (white) and 'B' (black) characters, and an integer k.

In a single operation, a white block can be recolored to black.

Return the minimum number of operations needed to produce at least one run of k consecutive black blocks.

Example 1:

Input: blocks = "WBBWWBBWBW", k = 7
Output: 3
Explanation:
One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks
so that blocks = "BBBBBBBWBW".
It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations.
Therefore, we return 3.

Example 2:

Input: blocks = "WBWBBBW", k = 2
Output: 0
Explanation:
No changes need to be made, since 2 consecutive black blocks already exist.
Therefore, we return 0.

Code

1
2
3