The Queue (Deque)
We use a double-ended queue (or standard queue) to store the indices of negative numbers currently in our sliding window.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Given an array and an integer K, find the first negative integer for each and every window (contiguous subarray) of size K.
We use a double-ended queue (or standard queue) to store the indices of negative numbers currently in our sliding window.
As the window slides right, the index at the front of the queue might fall out of the window. We must `shift()` it out if `deque[0] <= i - K`.
Once we've processed at least K elements, the front of the queue is guaranteed to be the FIRST negative number in our current window. If the queue is empty, there are no negatives.