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]?

  1. A. 2
  2. B. 1
  3. C. 4
  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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic