Data Structures & AlgorithmsVery hard
For the 'ramp width' problem (max j − i with A[i] ≤ A[j]), why are only prefix minima and suffix maxima worth considering?
- A. Only the global maximum can be a right end.
- B. Ramps always start at index zero.
- C. Prefix minima and suffix maxima are the same for every array.
- D. A left end that is not a prefix minimum is beaten by an earlier, smaller value that gives a wider ramp, and a right end not a suffix maximum is beaten by a later, larger value.
Correct answer: D. A left end that is not a prefix minimum is beaten by an earlier, smaller value that gives a wider ramp, and a right end not a suffix maximum is beaten by a later, larger value.
An earlier, smaller left candidate dominates a later one, so only decreasing prefix minima matter; a later, larger right candidate dominates an earlier one, so only suffix maxima matter. Two sorted lists then give a linear algorithm.
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 does pairing the heaviest remaining person with the lightest one (when they fit together) minimise boats when each boat carries at…
- The minimum number of consecutive deletions to make an array non-decreasing is found by which structure?
- Why is k ≥ n a necessary condition for the residues of n values modulo k to be distinct?
- When counting subsequences 'ace' in a string, why does a DP over the pattern prefixes avoid double counting?
- To count subarrays with equal numbers of even and odd values, which transformation makes the problem a zero-sum subarray problem?
- The function below computes the length of a longest increasing subsequence. python import bisect a = [4, 4, 1, 5, 5, 6, 2, 7] t = [] for x…