Data Structures & AlgorithmsVery hard
Why does the lexicographically largest subsequence after deleting k digits come from a greedy stack?
- A. The greedy always removes the largest digit.
- B. A smaller digit followed by a larger digit should be removed first, because keeping the earlier digit larger improves the string most.
- C. Sorting the digits in descending order is always the answer.
- D. Removing digits from the end is always optimal.
Correct answer: B. A smaller digit followed by a larger digit should be removed first, because keeping the earlier digit larger improves the string most.
The leftmost position dominates the string's order, so putting a larger digit earlier is the priority. A stack removes a smaller predecessor whenever a larger digit arrives, using the budget where it helps most.
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
- In a sliding window tracking the maximum number of vowels among windows of fixed length 3, why is O(1) update per step possible?
- Why does a circular window of length 3 require index arithmetic modulo n?
- Why is the number of Dyck-word stack sequences with n pushes and n pops equal to a Catalan number?
- A deque-based minimum of each sliding window runs in linear time because:
- 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…