Data Structures & AlgorithmsHardGATE CS · ICPC regional

Given a set of intervals, you want the fewest intervals to remove so the rest do not overlap. Which greedy strategy is correct?

  1. A. Sort by start time and keep the longest interval first
  2. B. Remove the interval with the most overlaps repeatedly
  3. C. Sort by end time and keep an interval whenever it starts after the last kept interval ends
  4. D. Sort by length and keep the shortest interval first

Correct answer: C. Sort by end time and keep an interval whenever it starts after the last kept interval ends

Choosing the earliest-finishing compatible interval leaves the most room; sorting by length or by start time can give a worse answer.

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

Exams that ask this topic