Data Structures & AlgorithmsVery hard
The minimum number of consecutive deletions to make an array non-decreasing is found by which structure?
- A. The longest decreasing subsequence.
- B. A non-decreasing prefix joined to a non-decreasing suffix, with two pointers matching the junction.
- C. The sum of all descents.
- D. Sorting the array and counting differences.
Correct answer: B. A non-decreasing prefix joined to a non-decreasing suffix, with two pointers matching the junction.
The kept elements form a non-decreasing prefix followed by a non-decreasing suffix whose junction is compatible. Two pointers find the widest compatible pair of prefix and suffix, and the deleted block is the gap between them.
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 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…
- A maximum subarray sum routine is written as follows. python def bestsub(a): cur = best = a[0] for x in a[1:]: cur = max(x, cur + x) best…
- A knapsack routine is written as follows (items are (weight, value)). python items = [(2, 3), (3, 4), (4, 5)] W = 6 dp = [0] (W + 1) for…