Meeting Rooms

Easy

Topics
ArraySortingIntervals

Given an array of meeting time intervals where intervals[i] = [start, end], return true if a person could attend all meetings (no two overlap).

Example 1

030[0,30][5,10][15,20]
The shaded lanes overlap, so one person cannot attend both and the answer is false. An end time equal to the next start is fine — the clash test is start < previous end, strictly.
Each lane is one interval drawn on a shared axis, so overlaps line up vertically.
Input:  intervals = [[0,30],[5,10],[15,20]]
Output: false

Example 2

210[7,10][2,4]
No two lanes overlap, so every meeting can be attended and the answer is true. Sorting by start time reduces the check to comparing each interval with only the one before it.
Input:  intervals = [[7,10],[2,4]]
Output: true

Constraints

  • 0 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start < end
Editorial — intuition, approach, complexity and pitfalls

Intuition

If you sort meetings by start time, a conflict can only happen between consecutive meetings — one starts before the previous ends.

Approach

  1. Sort intervals by start time.
  2. Scan adjacent pairs; if a meeting starts before the previous one ends, return false.
  3. If no such overlap, return true.

Dry Run

intervals = [0,30] [5,10] [15,20]
  sorted  [0,30] [5,10] [15,20]       (already in start order)
  [5,10]  starts at 5, previous ends at 30  ->  5 < 30, OVERLAP, return false
answer false
intervals = [7,10] [2,4]
  sorted  [2,4] [7,10]
  [7,10]  starts at 7, previous ends at 4   ->  7 < 4 is false, ok
answer true
Sorting first is what makes "compare neighbours only" sound. After the sort, if
any pair overlaps then some ADJACENT pair does, so three comparisons settle a
three-meeting schedule instead of all pairs.
It is worth being precise about what goes wrong without the sort, because the
direction is not the obvious one. Drop intervals.sort() and the scan cannot miss a
real conflict -- if every meeting starts at or after the previous one ends then the
input was already in start order, so the sort was a no-op anyway. What it does
instead is raise false alarms: [8,9] [0,1] is two disjoint meetings handed over out
of order, and the unsorted scan sees 0 < 9 and returns false. Over 60000 random
schedules, dropping the sort turned a clean schedule into a reported conflict 8385
times and never once hid a real conflict.
The comparison is strict. Touching is not overlapping: [1,3] [3,9] is legal and
returns true, while changing < to <= returns false for it.

Complexity

  • Time — O(n log n): dominated by the sort.
  • Space — O(n) in Java and Python, O(log n) in C++: the sweep keeps only the previous end time, so O(1) of its own, and the sort accounts for the rest. Note it is more than the O(log n) a primitive sort costs: this sorts int[][] through a comparator, and Arrays.sort on objects is Timsort, which allocates an O(n) merge buffer. Python's sort is Timsort too. Only C++ std::sort stays in place at O(log n) of recursion.

Common Pitfalls

  • Sort first, but for the right reason. Without the sort the scan cannot miss a conflict — if every meeting starts at or after the previous one ends, the input was already in start order. It raises false alarms instead: [8,9] [0,1] is two disjoint meetings given out of order, and the unsorted scan sees 0 < 9 and returns false. Across 60000 random schedules, dropping the sort reported a clean schedule as conflicting 8385 times and never once hid a real conflict.
  • Touching is not overlapping. One meeting ending exactly when the next begins is legal, so the comparison is strict <. With <=, [1,3] [3,9] reports false even though the two merely touch.

Full Solution — all languages

class Solution:
    def canAttendMeetings(self, intervals):
        intervals.sort()
        for i in range(1,len(intervals)):
            if intervals[i][0]<intervals[i-1][1]:
                return False
        return True
SensAI
Beta
Tap the mic to stop voice mode