Meeting Rooms – Solution & Complexity

Solution Walkthrough

  1. 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 i starts before meeting i - 1 finishes (intervals[i][0] < intervals[i - 1][1]), they overlap.
  2. Step 2: Single adjacent scan

    • Walk i from 1 to n - 1 and compare sorted[i][0] < sorted[i - 1][1].
    • Use strict < (not <=) because back-to-back meetings like [1, 5] and [5, 8] do not conflict.
  3. Step 3: Optimal O(n log n) sort and scan

    • Sorting dominates at O(n log n) time and O(n) (or O(1) in-place) extra space.
    def can_attend_meetings(intervals: list[list[int]]) -> bool:
        ordered = sorted(intervals, key=lambda x: x[0])
        for i in range(1, len(ordered)):
            if ordered[i][0] < ordered[i - 1][1]:
                return False
        return True
  4. Step 4: Common mistakes

    • Using <= instead of <, which wrongly flags touching meetings [1, 5] and [5, 8] as overlapping.
    • Assuming intervals is already sorted.

FAQ