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: 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.
Constraints:
n == blocks.length
1 <= n <= 100
blocks[i]
is either 'W'
or 'B'
.1 <= k <= n
This problem asks for the minimum number of operations needed to get at least one sequence of k
consecutive black blocks ('B') in a string of black and white blocks. An operation consists of changing a white block ('W') to a black block.
The most efficient approach is using a sliding window technique. We maintain a window of size k
and count the number of white blocks within that window. The minimum number of white blocks encountered across all possible windows represents the minimum number of recolors needed.
Algorithm:
Initialization:
k
at the beginning of the string blocks
.'W'
) within this initial window. This count represents the initial number of recolors needed (min_recolors
).Sliding the Window:
white_block_count
if the element leaving the window is 'W'.white_block_count
if the new element entering the window is 'W'.min_recolors
with the minimum of the current min_recolors
and the updated white_block_count
.Return the Result:
min_recolors
. This represents the minimum number of recolors required to achieve at least one sequence of k
consecutive black blocks.Time Complexity: O(n), where n is the length of the string blocks
. We iterate through the string once using a sliding window.
Space Complexity: O(1), as we only use a few constant extra variables to store the counts and the minimum number of recolors.
Code Examples:
The following code examples demonstrate the solution in several programming languages. They all follow the same algorithmic structure described above.
Python:
def minimumRecolors(blocks: str, k: int) -> int:
min_recolors = blocks[:k].count('W') # Initialize with white blocks in the first window
white_block_count = min_recolors
for i in range(k, len(blocks)):
white_block_count -= (blocks[i - k] == 'W') # Remove block leaving the window
white_block_count += (blocks[i] == 'W') # Add block entering the window
min_recolors = min(min_recolors, white_block_count) # Update minimum
return min_recolors
Java:
class Solution {
public int minimumRecolors(String blocks, int k) {
int minRecolors = 0;
for (int i = 0; i < k; i++) {
if (blocks.charAt(i) == 'W') {
minRecolors++;
}
}
int whiteBlockCount = minRecolors;
for (int i = k; i < blocks.length(); i++) {
whiteBlockCount -= (blocks.charAt(i - k) == 'W' ? 1 : 0);
whiteBlockCount += (blocks.charAt(i) == 'W' ? 1 : 0);
minRecolors = Math.min(minRecolors, whiteBlockCount);
}
return minRecolors;
}
}
C++:
#include <string>
#include <algorithm>
class Solution {
public:
int minimumRecolors(string blocks, int k) {
int min_recolors = 0;
for (int i = 0; i < k; ++i) {
if (blocks[i] == 'W') min_recolors++;
}
int white_count = min_recolors;
for (int i = k; i < blocks.length(); ++i) {
white_count -= (blocks[i - k] == 'W');
white_count += (blocks[i] == 'W');
min_recolors = min(min_recolors, white_count);
}
return min_recolors;
}
};
These examples are concise and demonstrate the core sliding window logic efficiently. Other languages would follow a similar pattern, adapting the syntax as needed.