↓Skip to main content

Sliding-Window

LeetCode 4067: Longest Subarray With Restricted Pair Sums
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 4067 note: subarrays of a valid window stay valid, so slide; adding x only needs checks involving x: x as an addend (x + y in the window) or x as the sum (x - y in the window)
LeetCode 1425: Constrained Subsequence Sum
·2 mins
LeetCode Hard Sliding-Window Dynamic-Programming Monotonic-Queue Heap Queue Array
LeetCode 1425 note: dp[i] = nums[i] + max(0, max of the previous k dp values); that max is a sliding window maximum, available in O(1) from a monotonic deque
LeetCode 719: Find K-th Smallest Pair Distance
·2 mins
LeetCode Hard Binary-Search Two-Pointers Sliding-Window Sorting
LeetCode 719 note: binary search on the distance, counting pairs with distance <= mid using a sliding window
LeetCode 3835: Count Subarrays With Cost Less Than or Equal to K
·2 mins
LeetCode Medium Sliding-Window Monotonic-Queue Queue Array
LeetCode 3835 note: cost = (max - min) × length only grows as the window grows, so a sliding window counts subarrays; two monotonic deques keep the window’s max and min
LeetCode 3323: Minimize Connected Groups by Inserting Interval
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3323 note: merge first; a new interval can join groups l through r ⇔ start of group r − end of group l <= k, so a sliding window finds the most groups one interval can connect
LeetCode 1984: Minimum Difference Between Highest and Lowest of K Scores
·1 min
LeetCode Easy Sliding-Window Sorting Array
LeetCode 1984 note: after sorting, the chosen k scores are always contiguous; slide a fixed window of length k and subtract its ends
LeetCode 658: Find K Closest Elements
·3 mins
LeetCode Medium Two-Pointers Binary-Search Sliding-Window
LeetCode 658 note: shrink two pointers from both ends to length k, or binary search the window start
LeetCode 1156: Swap For Longest Repeated Character Substring
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 1156 note: for each character c, find the longest window with at most one non-c; the swapped-in c must come from elsewhere, so the length is capped at the total count of c
LeetCode 2401: Longest Nice Subarray
·2 mins
LeetCode Medium Sliding-Window Bit-Manipulation Array
LeetCode 2401 note: pairwise AND 0 ⇔ each bit is used by at most one number; a mask holds the window’s bits, and on a conflict the left shrinks and its bits are removed
LeetCode 992: Subarrays with K Different Integers
·2 mins
LeetCode Hard Sliding-Window Hash-Table Counting Array
LeetCode 992 note: “exactly k kinds” = “at most k” − “at most k - 1”, where each at-most window adds right - left + 1 per right end
LeetCode 3306: Count of Substrings Containing Every Vowel and K Consonants II
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 3306 note: “exactly k consonants” is not monotonic; compute “all five vowels and at least k consonants” minus “at least k + 1”, both “at least” counting windows
LeetCode 930: Binary Subarrays With Sum
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 930 note: “sum exactly goal” = “sum at most goal” − “sum at most goal - 1”; or prefix sums with a hash map