Sliding window is a technique used to reduce the time complexity of algorithms by eliminating the need to repeatedly loop through a structure.
Static Sliding Window
The static sliding window algorithm uses a window of fixed size that moves through the structure.
Algorithm
- Determine window size
- Initialization and initial calculations
- Repeat
- Slide the window
- Update state, evaluate window
- Return result
Complexity
Time complexity is usually linear or close to linear - O(n)
Space complexity is O(1) because the amount of things stored in memory is constant, regardless of input size.
Example
finding the maximum subarray sum
public static int maxSubarraySum(int[] numbers, int subLength)
int maxSum = Interger.MIN_VALUE;
int windowSum = 0;
// initial window calculation
for (int i = 0; i<k ; i++) {
windowSum += numbers[i]
}
//sliding and evaluation
for (int i = subLength; i < numbers.length; i++) {
windowSum += numbers[i] - numbers[i-k]
maxSum = Math.max(maxSum, windowSum)
}
return maxSum
Dynamic Sliding Window
In this version of the Sliding Window Algorithm, the window is determined by start and end pointers, and the size of the window is changing according to the needs
Algorithm
- Initialization
- Move the end of the window, making it bigger
- Evaluate window
- if appropriate move the start of the window, making it smaller