Pattern 02 of 08 · sliding-window
Sliding Window
Maintain an active contiguous subarray/substring window that grows and shrinks monotonically to track optimum metrics in O(N) time.
- Builds on
- Two Pointers
- Unlocks
- The end of this branch.
See the invariant
Step through itPattern InvariantSliding Window
Maximum Sum Subarray of Fixed Size K = 3
Max: 8
Step 1 of 4L
2
[0]1
[1]R
5
[2]1
[3]3
[4]2
[5]Window Slice: [2 + 1 + 5] = 8
Initial window of size K=3 established: [2, 1, 5]. Sum = 8. Max recorded = 8.
Speed: