Sliding Window 🪟

Here, I provide a template for sliding window that can handle most questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function slidingWindow(nums) {
const length = nums.length;
const WINDOW_SIZE = K;
// Sum is optional.
let sum = 0;
let ans = 0;

for (let left = 0, right = 0; right < length; right++) {
sum += nums[right];
// The condition here is flexible.
while (left < right && right - left + 1 >= WINDOW_SIZE) {
sum -= nums[left];
left++;
}
ans = Math.max(ans, right - left + 1);
right++;
}
}