Data Structures & AlgorithmsVery hard
In the greedy patching algorithm, the reachable range is [1..R] and the next sorted value x exceeds R+1. Why is R+1 the right patch?
- A. R+1 is the smallest value that is not reachable, and adding it extends the reachable range to 2R+1, the largest extension a single patch can give.
- B. Any value above R is always cheaper to add, so R+1 is never needed.
- C. A patch must always equal the sum of all current elements.
- D. R+1 is the only number that can be formed by an odd subset.
Correct answer: A. R+1 is the smallest value that is not reachable, and adding it extends the reachable range to 2R+1, the largest extension a single patch can give.
If R+1 is unreachable then it must be patched, and with [1..R] reachable adding R+1 makes every value up to 2R+1 reachable. Larger patches leave R+1 unreachable and so cost more later.
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
- A monotonic deque for sliding-window maximum stores array INDICES instead of values. Why?
- When tracking a '' wildcard that may be '(', ')' or empty while scanning brackets, why is it enough to keep a range [lo, hi] of possible…
- Two different strings produced equal rolling-hash values. What does this tell you?
- In iterative reversal of a singly linked list, which pointer must be saved BEFORE the current node's next pointer is changed?
- For sorted positive integers, the smallest value that cannot be formed as a subset sum equals R+1, where R is the reach. Why does a value…
- Items (weight, value) are [(5, 60), (4, 40), (6, 50), (3, 30), (2, 15), (7, 70)]. Each item can be taken at most once, and the total…