2379. Minimum Recolors to Get K Consecutive Black Blocks
2379. Minimum Recolors to Get K Consecutive Black Blocks
Description
You are given a 0-indexed string blocks
of length n
, where blocks[i]
is either 'W'
or 'B'
, representing the color of the i^th
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:
1 | Input: blocks = "WBBWWBBWBW", k = 7 |
Example 2:
1 | Input: blocks = "WBWBBBW", k = 2 |
Constraints:
n == blocks.length
1 <= n <= 100
blocks[i]
is either'W'
or'B'
.1 <= k <= n
Hints/Notes
- 2024/10/06
- sliding window
- 0x3Fâs solution(checked)
Solution
Language: C++
1 | class Solution { |