Non-overlapping Intervals - Problem

Imagine you're a conference organizer trying to schedule meetings in rooms, but some meetings have overlapping time slots. Your goal is to find the minimum number of meetings to cancel so that no two remaining meetings overlap in time.

Given an array of intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Important: Intervals that only touch at a single point are considered non-overlapping. For example, [1, 2] and [2, 3] are perfectly fine together!

Example: If you have meetings [[1,2], [2,3], [3,4], [1,3]], you only need to remove 1 meeting (either [1,2] or [1,3]) to make all remaining meetings non-overlapping.

Input & Output

example_1.py โ€” Standard Case
$ Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
โ€บ Output: 1
๐Ÿ’ก Note: We can remove [1,3] to make the remaining intervals non-overlapping: [[1,2],[2,3],[3,4]]. Note that [1,2] and [2,3] don't overlap since they only touch at point 2.
example_2.py โ€” Multiple Overlaps
$ Input: intervals = [[1,2],[1,2],[1,2]]
โ€บ Output: 2
๐Ÿ’ก Note: All intervals are identical and overlapping. We need to remove 2 intervals to keep just one: [[1,2]].
example_3.py โ€” No Overlaps
$ Input: intervals = [[1,2],[2,3]]
โ€บ Output: 0
๐Ÿ’ก Note: The intervals [1,2] and [2,3] only touch at point 2, so they're non-overlapping. No removals needed.

Visualization

Tap to expand
๐Ÿข Conference Room Scheduler9AM10AM11AM12PM1PM2PM๐Ÿ“… Meeting Requests:Team A (9-10)Team B (10-11)Team C (11-12)Team D (9-11) โŒ CANCEL๐ŸŽฏ Greedy Strategy:โœ… Keep Team A (ends earliest)โŒ Cancel Team D (conflicts with A)โœ… Keep Team B (starts after A)โœ… Keep Team C (starts after B)๐Ÿ† Result: 1 meeting cancelled๐Ÿ’ก Key InsightAlways keep the meeting thatends earliest - it leaves themost room for future meetings!
Understanding the Visualization
1
Sort by End Time
Arrange meetings by when they finish - earliest finishers first
2
Keep the First
Always keep the meeting that ends earliest
3
Check for Conflicts
For each subsequent meeting, see if it starts before the last kept meeting ends
4
Make Greedy Choice
Cancel conflicting meetings, keep non-conflicting ones
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because keeping intervals that end earliest always leaves maximum space for future non-overlapping intervals, guaranteeing the minimum number of removals.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n * n)

2^n subsets to generate, each taking O(n) time to validate

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for recursion stack and storing current subset

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค intervals.length โ‰ค 105
  • intervals[i].length == 2
  • -5 ร— 104 โ‰ค starti < endi โ‰ค 5 ร— 104
  • Important: Intervals touching at endpoints are non-overlapping
Asked in
Amazon 42 Google 38 Microsoft 31 Meta 29 Apple 15
67.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen