2379: Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks
Easy

Nothin’ too special; just a man with a sliding window and a dream.

Code:

class Solution {
public:
    int minimumRecolors(string blocks, int k) {
        int ans = 0;
        
        // keep track of the number of black blocks
        // in the first window
        int black = 0;
        for (int i = 0; i < k; ++i) {
            if (blocks[i] == 'B') {
                ++black;
            }
        }
        ans = black;

        // use a sliding window, and keep updating
        // the counter of black blocks
        for (int i = k; i < blocks.size(); ++i) {
            if (blocks[i] == 'B') {
                ++black;
            }
            if (blocks[i-k] == 'B') {
                --black;
            }
            ans = max(ans, black);
        }

        // finally, subtract `ans` from the target number
        // of `k` blocks, to get the *minimum* number of 
        // white blocks we need to recolour
        return k-ans;
    }
};

Complexity: