LeetCode Patterns

Sliding Window Visualizer

Two pointers, one pass. The window expands to include new elements and contracts when a condition breaks — the aggregate updates incrementally instead of being recomputed.

Algorithm studio

Choose a pattern, tune the input, then scan it step by step.

Ready

01 · Select the challenge

02 · Configure the signal

Data lengthn = 12
Edit a value by selecting its cell before a run.

03 · Launch

LeetCode 643 (variant)O(n)
InitializeConfigure an input and start the scanner to reveal how the window moves.

Live decision inspector

Why does the window move?

Pending

What should the window do?

Waiting for the first element

Next action

Start the scan

Each step evaluates one condition before a pointer moves or a result is recorded.

Invariant

The window always represents one contiguous range.

Scrubbable timeline

Replay any decision

expandshrinkrecord

The timeline appears after a scan begins.

Every expansion, contraction, and best-answer update becomes a replayable checkpoint.

Live scan lane

Max Sum Subarray of Size K

Window
Length0
Sum0
0
1
2
3
4
5
6
7
8
9
10
11
Active windowEntering at RLeaving at LBest range
Each value crosses the scanner at most twice

Execution trace

Code in motion

def max_sum_k(nums, k):
left, win_sum, best = 0, 0, float('-inf')
for right in range(len(nums)):
win_sum += nums[right] # expand
if right - left + 1 > k:
win_sum -= nums[left] # shrink
left += 1
if right - left + 1 == k:
best = max(best, win_sum) # record
return best

Operation budget

0 sliding ops / ≤ 24

30brute force

Sliding window reuses state instead of recomputing every contiguous range.

Complexity race

Sliding window vs. brute force

Both lanes receive the same operation budget. Watch the sliding window reuse its state while brute force repeatedly rebuilds candidate ranges.

Shared budget0 operations
Potential savings6 operations

Sliding window

Reuses sum or frequency state

0 / 24
13313413714918936

Brute force

Rebuilds every candidate range

0 / 30
13313413714918936

Brute-force workbench

Start the scan to give both algorithms the same operation budget.

For small inputs either approach may finish quickly. The gap becomes decisive as n grows: sliding window scales linearly while enumerating contiguous ranges grows quadratically.

What is the Sliding Window Pattern?

The Sliding Window pattern maintains a contiguous range of an array or string between two pointers — left and right — together with an aggregate of the range's contents (a sum, a count, or a frequency map). Instead of recomputing the aggregate for every possible subarray (O(n²) or worse), the window updates it incrementally: add one element when the right pointer expands, remove one when the left pointer contracts. Each element enters and leaves at most once, so the total work is O(n).

Fixed vs. Variable Windows

  • Fixed-size: the window always spans exactly k elements — slide both pointers together (max sum of size k, averages, anagram checks).
  • Variable-size: grow until a condition breaks, then shrink until it holds again (smallest subarray ≥ target, longest substring without repeats).
  • Recognize it: the problem asks about a contiguous subarray/substring and some best/count over all of them.

Classic Problems

  • LeetCode 3 — Longest Substring Without Repeating Characters
  • LeetCode 209 — Minimum Size Subarray Sum
  • LeetCode 424 — Longest Repeating Character Replacement
  • LeetCode 567 — Permutation in String