Data Structures & AlgorithmsVery hard
Which statement about a monotonic deque for sliding-window minimum is correct?
- A. Each index is pushed and popped at most once, so the total work over the whole scan is linear.
- B. Each window costs O(k) work, so the total is O(nk).
- C. The deque must be rebuilt from scratch for every window.
- D. The deque stores the values in decreasing order and keeps the largest one at the back.
Correct answer: A. Each index is pushed and popped at most once, so the total work over the whole scan is linear.
Every element enters and leaves the deque at most once, which keeps the scan linear. Rebuilding for each window or storing values in the wrong order breaks the amortized bound.
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
- Why is prefix-sum counting of zero-sum subarrays unaffected by the choice of starting index for the prefix array?
- For the sum-at-most-K problem on a prefix-sum array with negative values, why does a balanced search structure appear in the solution?
- The shunting-yard algorithm converts the infix expression "a+bc^d^e-f^(gh)" to postfix, with ^ highest precedence and right-associative…
- What is the largest rectangular area in a histogram with bar heights 6, 2, 5, 4, 5, 1, 6 (each bar has width 1)?
- In the undirected graph with edges (1,2), (1,3), (2,4), (3,4), (4,5), (3,6), how many vertices are at shortest-path distance exactly 2…
- What is the minimum number of knight moves needed to go from square (0, 0) to (3, 3) on an 8 by 8 board (coordinates 0 to 7)?