You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B', representing the color of the ith block. The characters 'W' and 'B' denote the colors white and black, respectively.
You are also given an integer k, which is the desired number of consecutive black blocks.
In one operation, you can recolor a white block such that it becomes a black block.
Return the minimum number of operations needed such that there is at least one occurrence of k consecutive black blocks.
Example 1:
Input: blocks = "WBBWWBBWBW", k = 7
Output: 3Explanation: Recolor the 0th, 3rd, and 4th blocks to get 7 consecutive black blocks.
Example 2:
Input: blocks = "BWWWBB", k = 6
Output: 3Explanation: Recolor all the white blocks with black.
Constraints:
1 <= k <= blocks.length <= 100blocks[i] is either 'W' or 'B'.Before attempting this problem, you should be comfortable with:
We need to find a window of k consecutive blocks that requires the fewest recolors to become all black. A recolor is needed for each white block ('W') in the window. We can check every possible window of size k and count the white blocks in each, keeping track of the minimum count.
res to the length of the string (worst case).i from 0 to n - k:i to i + k - 1.res with the minimum count seen.res.When sliding the window one position to the right, most of the count stays the same. We only need to subtract the contribution of the element leaving the window and add the contribution of the new element entering. This avoids recounting the entire window each time, reducing time from O(n*k) to O(n).
0 to k-1). Set this as the initial result.k to n-1:i - k) is 'W', decrement the count.i) is 'W', increment the count.res with the minimum count seen.res.class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
count_w = 0
for i in range(k):
if blocks[i] == 'W':
count_w += 1
res = count_w
for i in range(k, len(blocks)):
if blocks[i - k] == 'W':
count_w -= 1
if blocks[i] == 'W':
count_w += 1
res = min(res, count_w)
return resThe sliding window must have exactly k elements. A common bug is using i <= len(blocks) - k vs i < len(blocks) - k + 1 incorrectly, causing either one too few or one too many windows to be checked. Trace through a small example like blocks = "WBB", k = 2 to verify your loop bounds.
When sliding the window, you must both remove the contribution of the element leaving the window and add the contribution of the element entering. Forgetting either update causes the count to drift from the actual number of white blocks in the current window.