Minimum Recolors to Get K Consecutive Black Blocks

You are given a **0-indexed** string `blocks` of length `n`, where `blocks[i]` is either `'W'` (a white block) or `'B'` (a black block). You are also given an integer `k`, the number of consecutive black blocks you want. In one operation you may recolor a white block so that it becomes black. Return *the **minimum** number of operations needed so that there is at least **one** occurrence of `k` consecutive black blocks.*

30:00

Minimum Recolors to Get K Consecutive Black Blocks
easy
Free written solution
Topics
Companies

You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' (a white block) or 'B' (a black block). You are also given an integer k, the number of consecutive black blocks you want.

In one operation you may recolor a white block so that it becomes black.

Return the minimum number of operations needed so that there is at least one occurrence of k consecutive black blocks.

Example 1:
Input: {"blocks":"WBBWWBBWBW","k":7}
Output: 3
Constraints:
  • n==blocks.lengthn == \text{blocks.length}

  • 1≤n≤1001 \leq n \leq 100

  • blocks[i]\text{blocks}[i] is either 'W' or 'B'.

  • 1≤k≤n1 \leq k \leq n

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"blocks":"WBBWWBBWBW","k":7}

Any run of 7 blocks can be made all black by painting its white blocks, so the cost of a window is its white count. Count the whites in the first window.

k = 7

0

1

2

3

4

5

6

7

8

9

paint needed for this window: 0

window is already all black

whites per window start (lower is better)

whites

0

best

0

Variables
VariableValue
k7
n10
DepthFunction Call
Stack empty
0/15