Data Structures & AlgorithmsVery hard

A deque-based minimum of each sliding window runs in linear time because:

  1. A. Each window is sorted with a fast sort.
  2. B. Each index enters and leaves the deque at most once, so the total work is proportional to the array length.
  3. C. The deque stores every element of every window.
  4. D. The minimum is always at the start of the array.

Correct answer: B. Each index enters and leaves the deque at most once, so the total work is proportional to the array length.

Indices are appended once and removed at most once from either end, so the amortized cost per element is constant. Sorting each window or storing all elements would cost much more.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic