Understanding the fixed sized sliding window pattern
Some problems require us to remember the occurrences of data items in a sequence of data for all windows of a fixed size. While we can use nested loops to generate a window of size k from each index, using the sliding window technique and a hash table to map data items in a window to some value can efficiently solve such problems. The value to which the data items of a window are mapped is defined by the problem, which, in most cases, is the frequency of data items in the window, but it could also be something else.
We maintain a fixed-sized window and move it through the sequence, adding the contributions of new items that get added and removing the contributions of the old items that get removed when the window moves.
Liking the course? Check our discounted plans to continue learning.