- Denisse's Newsletter
- Posts
- Day 26: Sliding window
Day 26: Sliding window

Hello and welcome back brave human being,
To the new ones here, welcome!!
Happy 4th of July!
I hope you're enjoying your day off.
I'm currently traveling to Mexico ☀️⛱️, and during my travels, I couldn't help but wonder...
Am I addicted to the excitement of doing things at the last minute?
It's currently 2 am, just a couple of hours before I need to deliver this newsletter to you, and here I am typing it the night before.
I am the queen of edge cases. Most of these could’ve been avoided if I had done things with more time and care.
While I often don’t apply this daily, having so much experience with edge cases has helped me be creative in my interviewing skills and has given me the habit of checking everything twice.
So, is this a flaw or a superpower?
I guess it’s one of those glass-half-full or half-empty situations.
So, my message today is: Embrace your quirks and use them to your advantage.
You are given a string that is formed of characters 'W
' (white) and 'B
' (black), and an integer k
. k
represents the desired number of consecutive black blocks.
Find the minimum number of operations needed to recolor white blocks to black blocks to achieve k
consecutive black blocks.
A recolor is when we convert a W
→ B
to obtain consecutively black blocks.
Before we start, there are two main keywords that the problem gives us; k
and minimum.
k
is a sub-set of our string or can also be called our window.
Minimum is what is going to determine which k
(window) is our answer.
Let’s dive in 🤿,
First let’s define our variables,
minRecolors
: This variable will store the minimum number of recolors needed.whiteBlocks
: This variable will keep track of the number of white blocks in the current window.L
: This pointer will indicate the start of our sliding windowR
: This pointer will indicate the end of our sliding window and help us expand the window size tok
.
The sliding window technique involves iterating through our string using the L
and R
pointers. R
will sliiide through the string (iterate with a for loop).
At each step of the iteration, we will check if the character at R
is W
. If it is, we increase the whiteBlocks
counter.
We will also check if the current window size is k
using:
R - L + 1 == k
If the window is of size k
, we update minRecolors
with the current whiteBlocks
value if it is smaller than the previous minRecolors
.
We then slide the window by incrementing L
and adjusting whiteBlocks
accordingly if the character at L
was W
.

Final Answer:
class Solution {
public:
int minimumRecolors(string blocks, int k) {
int minRecolors = INT_MAX;
int whiteBlocks=0;
int l=0;
for(int r=0; r<blocks.size(); r++){
if(blocks[r]=='W'){
whiteBlocks++;
}
if(r-l+1==k){ //The size or our window
minRecolors = min(minRecolors,whiteBlocks);
if(blocks[l]=='W'){
whiteBlocks--;
}
l++;
}
}
return minRecolors;
}
};
What is the time complexity? |
HACKS OF THE WEEK
Get free tickets to conferences through newsletters! I recently attended CloudNativeSecurityCon, which had a value of $800, for free 🤑 simply by signing up for the Meetup newsletter. (You never know when there is gold in your email)
I recently found this ebook through TikTok, been reading it, has helped me relax.
I have been sleeping with my mouth taped. It has worked great so far!
SELF-GASLIGHT OF THE WEEK
I truly believe you can gaslight yourself to success. This is what I have been using this week to gaslight myself.
I gaslight myself into walking after hearing: The Power Of Long Walks (15,000 Steps A Day Changed My Life) by Dan Koe
I gaslight myself into building a personal brand by watching Courtney Johnsons Tik Toks
THIS WEEK’S MANTRA
See ya next week!
Denisse
Reply