Data Structures & AlgorithmsVery hard
A deque-based minimum of each sliding window runs in linear time because:
- A. Each window is sorted with a fast sort.
- B. Each index enters and leaves the deque at most once, so the total work is proportional to the array length.
- C. The deque stores every element of every window.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many ways are there to make 15 using coins of values 1, 2, 5 and 10, where the order of coins does not matter (so 5+5+5 and 5+10 are…
- A binary string "1011001010" should become non-decreasing (all 0s before all 1s). In one operation you flip a single character. What is…
- What is the length of the longest NON-DECREASING subsequence (equal neighbours allowed) of [5, 1, 3, 3, 2, 6, 6, 4, 7]?
- How many subsets of [2, 3, 5, 4, 6] (chosen by position) have a sum exactly equal to half of the total sum?
- A staircase has 8 steps. Each move climbs 1 or 2 steps, but you may NOT take three single-step moves in a row. How many different…
- In the binary array [1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run of…