Meeting Rooms – Solution & Complexity
Solution Walkthrough
Step 1: Sort by start time
- Checking every pair takes
O(n^2). - If you sort the intervals by start time, any overlap must appear between two adjacent intervals: if meeting
istarts before meetingi - 1finishes (intervals[i][0] < intervals[i - 1][1]), they overlap.
- Checking every pair takes
Step 2: Single adjacent scan
- Walk
ifrom1ton - 1and comparesorted[i][0] < sorted[i - 1][1]. - Use strict
<(not<=) because back-to-back meetings like[1, 5]and[5, 8]do not conflict.
- Walk
Step 3: Optimal O(n log n) sort and scan
- Sorting dominates at
O(n log n)time andO(n)(orO(1)in-place) extra space.
- Sorting dominates at
Step 4: Common mistakes
- Using
<=instead of<, which wrongly flags touching meetings[1, 5]and[5, 8]as overlapping. - Assuming
intervalsis already sorted.
- Using
Part of Blind 75 · NeetCode 150