Data Structures & AlgorithmsHardCodeforces Div2 C · LeetCode-style
A garden of length 5 has taps at positions 0 to 5. Tap i waters the interval [i − ranges[i], i + ranges[i]] where ranges = [3, 4, 1, 1, 0, 0]. What is the minimum number of taps to water the whole garden [0, 5]?
- A. 2
- B. 1
- C. 4
- D. 0
Correct answer: B. 1
Greedy interval cover: sort intervals by start and, from the current covered point, pick the interval reaching farthest among those that start by then. Picking any interval that covers the next uncovered point (not the farthest) can use one extra tap.
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
- From the number 1432219 you must delete exactly 3 digits (the remaining digits keep their order). What is the smallest number you can…
- What is the minimum number of swaps of two (not necessarily adjacent) elements needed to sort the array [4, 3, 2, 1, 6, 5] into increasing…
- People weigh [3, 5, 3, 4, 2, 7, 1] kg. Each boat carries at most two people and at most 9 kg in total. What is the minimum number of boats…
- Children stand in a row with ratings [1, 2, 2, 5, 4, 3, 3, 8, 1]. Each child gets at least one candy, and a child with a higher rating…
- Jobs of lengths [7, 6, 5, 4, 3, 3] are assigned to 2 identical machines. Each job runs on one machine without interruption. What is the…
- Intervals (half-open, [start, end)) are [(1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8)]. What is the minimum number of intervals you must…