Count Subarrays With Median K — Prefix Balance Reduction
LeetCode 2488 asked at Google and Amazon. Map elements to +1/-1 relative to k and count prefix balances of 0 or 1 around k in O(n) time.
1582 articles
LeetCode 2488 asked at Google and Amazon. Map elements to +1/-1 relative to k and count prefix balances of 0 or 1 around k in O(n) time.
LeetCode 480 asked at Google, Amazon, and Bloomberg. Maintain a max-heap and a min-heap with lazy deletion to compute the median in O(n log k) time.
LeetCode 30 asked at Google, Amazon, and Meta. Run a word-aligned sliding window for each of the wlen possible offsets to find every concatenation start in O(n*wlen) time.
LC 2260 asks for the shortest consecutive sequence of cards containing a matching pair. Track the last-seen index of each card value — update the minimum window each time a duplicate is encountered. O(n) time, O(n) space.
Count subarrays where score = sum × length is less than k using a shrinkable sliding window with a running sum. Counting all valid subarrays ending at each right pointer is the key trick that avoids an inner loop.