Data Structures & AlgorithmsHardCodeforces Div2 C · LeetCode-style

Intervals (inclusive integer ranges) are [(1, 3), (2, 5), (4, 9), (6, 7), (8, 12), (9, 10)]. What is the minimum number of intervals whose union covers every integer from 1 to 12?

  1. A. 4
  2. B. 1
  3. C. 5
  4. D. 3

Correct answer: D. 3

Greedy cover: from the first uncovered point, choose among intervals starting no later than it the one reaching farthest right, then repeat from one past that end. Choosing the first interval that covers the point (rather than the farthest) can need one extra interval.

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

Exams that ask this topic