Meeting Rooms (LeetCode 252): Solution and Explanation

Easy
Array
Sorting

You are given a list of meeting time intervals, each with a start and an end. Decide whether a single person could attend all of them, which is true only if no two meetings overlap.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

The direct approach compares every pair of meetings and checks whether they overlap. Two intervals [a, b] and [c, d] overlap when each starts before the other ends. That is correct, but with ten thousand meetings it means about fifty million pair checks, and it does not use any structure in the data.

Think about how you would check a calendar by hand. You would read it in time order and, for each meeting, only ask whether it begins before the previous one ends. Once meetings are sorted by start time, a meeting can only clash with something that started earlier, and among those the most recent one is the only one that matters. If the previous meeting finished in time, every earlier one finished even sooner, because they were already verified not to overlap with their own successors. In other words, sorting lets each meeting be checked against exactly one other meeting instead of all of them.

So sorting turns a global pairwise check into a single pass over adjacent pairs. The only question left is how to treat touching endpoints. A meeting that ends at 10 and another that starts at 10 do not conflict, since you can walk out of one and into the next, so the overlap test is a strict comparison: the previous end must be greater than the next start.

Approach: Sort by start time and compare neighbors

  1. Sort intervals by their start time.
  2. Loop i from 1 to the last index, looking at the pair intervals[i - 1] and intervals[i].
  3. If the earlier meeting's end i1[1] is greater than the next meeting's start i2[0], they overlap, so return false.
  4. If the loop finishes without a conflict, return true. An empty list or a single meeting also returns true.

Step-by-step example

intervals = [[0,30],[5,10],[15,20]]
  1. Sorting by start leaves the order as [[0,30],[5,10],[15,20]].
  2. i = 1: compare [0,30] with [5,10]. The first ends at 30, which is greater than the second's start of 5, so they overlap.
  3. Return false. The person cannot attend both the long meeting and the one at 5.
  4. For contrast, [[7,10],[2,4]] sorts to [[2,4],[7,10]], and 4 is not greater than 7, so that input returns true.

Code: Python, Java, C++

1class Solution:
2    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
3        intervals.sort(key = lambda i: i[0])
4        
5        for i in range(1, len(intervals)):
6            i1 = intervals[i - 1]
7            i2 = intervals[i]
8            
9            if i1[1] > i2[0]:
10                return False
11        return True

Time and space complexity

Time

O(n log n)

Sorting dominates at O(n log n). The scan over adjacent pairs afterward is O(n).

Space

O(1)

The scan uses constant extra space. The sort itself may use up to O(n) auxiliary memory depending on the language, such as Python's Timsort. No extra data structures are built.

Other approaches

Compare every pair (time O(n²), space O(1))

For each pair of meetings, check whether a.start < b.end and b.start < a.end. No sorting is needed, but it is quadratic and only practical for small inputs.

Sweep over separately sorted starts and ends (time O(n log n), space O(n))

Sort start times and end times separately, then walk both and fail if the number of active meetings ever exceeds one. It is overkill here but is exactly the technique used for the follow-up that counts rooms.

Common mistakes

  • Using >= in the overlap check. A meeting ending at 10 and another starting at 10 are back to back, not overlapping, so the comparison must be strict.
  • Forgetting to sort first, which makes the neighbor check meaningless for unordered input.
  • Sorting by end time and then comparing the wrong fields. With the start-time sort, always compare the previous end with the next start.
  • Not handling an empty list. The loop starting at 1 handles it naturally, but code that reads intervals[0] up front will crash.

Frequently asked questions

Why do we sort meetings by start time?

After sorting, any conflict must show up between neighbors. If a meeting starts after the previous one ends, it also starts after every earlier meeting ends, so a single pass over adjacent pairs checks the whole schedule.

Do meetings that touch at an endpoint overlap?

No. A meeting that ends at time t and another that starts at time t can both be attended, which is why the code uses a strict > comparison.

What is the time complexity of meeting rooms?

It is O(n log n) because of the sort. The comparison pass is linear and uses constant extra space.

How is this different from Meeting Rooms II?

This version only asks whether any overlap exists. The follow-up asks how many rooms are needed at the busiest moment, which requires counting concurrent meetings with a sweep or a min-heap of end times.