Loading section...
Non-Overlapping Intervals: Greedy by End Time
Concepts: pyIntervalScheduling, pyGreedyEndTime
This problem has two equivalent framings. 'Find the minimum number of intervals to remove so no two intervals overlap.' And: 'Find the maximum number of non-overlapping intervals you can keep.' These are the same problem: max_keep = n - min_remove. Focus on maximizing what you keep. This is the classic interval scheduling maximization problem from algorithm theory, and the greedy strategy is proven optimal. Why Sort by End Time? The greedy insight is this: always keep the interval that ends earliest. Why? Because an interval with an earlier end time leaves more room for future intervals. An interval that ends at 5 is strictly better than one that ends at 8 when you are trying to fit as many non-overlapping intervals as possible into the future. This is counterintuitive at first — why not s