↓Skip to main content

Sliding-Window

LeetCode 3298: Count Substrings That Can Be Rearranged to Contain a String II
·2 mins
LeetCode Hard Sliding-Window Hash-Table String
LeetCode 3298 note: rearrangeable to start with word2 ⇔ every character count is at least word2’s; an “at least” counting window where meet tracks how many characters have reached their target
LeetCode 2537: Count the Number of Good Subarrays
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2537 note: adding x creates cnt[x] new equal pairs and removing it loses cnt[x] - 1; an “at least k pairs” counting window with count += left
LeetCode 3325: Count Substrings With K-Frequency Characters I
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 3325 note: an “at least one character appears k times” counting window; when the new character reaches k, keep shrinking until it drops to k - 1
LeetCode 2962: Count Subarrays Where Max Element Appears at Least K Times
·2 mins
LeetCode Medium Sliding-Window Array
LeetCode 2962 note: an “at least k” counting window: keep shrinking while the max appears k times, after which every left end in [0, left) is valid, so count += left
LeetCode 2762: Continuous Subarrays
·2 mins
LeetCode Medium Sliding-Window Monotonic-Queue
LeetCode 2762 note: a sliding window with two monotonic queues for the window maximum and minimum
LeetCode 1358: Number of Substrings Containing All Three Characters
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 1358 note: an “at least one each of a, b, c” counting window: keep shrinking while all three are present, then count += left
LeetCode 632: Smallest Range Covering Elements from K Lists
·3 mins
LeetCode Hard Heap K-Way-Merge Sliding-Window
LeetCode 632 note: a heap of each list’s current element plus the running maximum; always advance the smallest
LeetCode 3258: Count Substrings That Satisfy K-Constraint I
·2 mins
LeetCode Easy Sliding-Window String
LeetCode 3258 note: valid means zeros <= k or ones <= k; it is invalid only when both exceed k, and after shrinking there are right - left + 1 valid substrings ending at right
LeetCode 2968: Apply Operations to Maximize Frequency Score
·2 mins
LeetCode Hard Sliding-Window Sorting Prefix-Sum Binary-Search Array
LeetCode 2968 note: after sorting, the cheapest common target for a block is its median; prefix sums give the cost in O(1), and it grows with the window, so slide
LeetCode 2904: Shortest and Lexicographically Smallest Beautiful String
·2 mins
LeetCode Medium Sliding-Window String
LeetCode 2904 note: with exactly k ones in the window, also shrink away leading zeros to get the shortest beautiful substring ending at right; compare by length first, then lexicographically
LeetCode 2875: Minimum Size Subarray in Infinite Array
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 2875 note: strip target // sum full cycles; the remainder is smaller than one cycle, so a sliding window over two copies of nums always finds it
LeetCode 2302: Count Subarrays With Score Less Than K
·2 mins
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search Array
LeetCode 2302 note: score = sum × length with positive numbers grows with the window; shrink while the score is >= k, and right - left + 1 valid subarrays end at right