Sliding Window Technique
February 19, 2021•104 words
I've recently figured out a basic template on how to solve these types of problems:
- Create a 'window' that will store the information of things from index 'start' to index 'end'
- Let 'end' be the iterator that loops through the entire array
- For each iteration add the element at 'end' to 'window'
- While the question's condition is not met, reduce the size of 'window' by removing the element at 'start'
- After the while, the condition is met so update the result as necessary.
You can identify sliding window problems with keywords such as 'longest sub array that has x'