← DSA
EASY

Meeting Rooms

PracticeNeetCode ↗LeetCode ↗

Given an array of meeting time intervals, determine whether one person could attend every meeting — i.e. whether any two intervals overlap.

Example

Input intervals = [[0,30],[5,10],[15,20]]
Output false

Key Pointers

Step-by-Step Analysis

  1. Sort the intervals by start time.
  2. Walk adjacent pairs and check whether the next interval's start is before the current interval's end.
  3. If any pair overlaps, return False.
  4. If the walk finishes with no overlaps, return True.

Complexity

Time: O(n log n) for the sort — Space: O(1) extra

Code

def canAttendMeetings(intervals: list[list[int]]) -> bool:
    intervals.sort(key=lambda x: x[0])

    for i in range(1, len(intervals)):
        if intervals[i][0] < intervals[i - 1][1]:
            return False

    return True

Code Explanation

How the solution above works, piece by piece.

  1. Sort meetings by start time.
  2. If any meeting starts before the previous one ends, they overlap → False.
  3. Only neighbours need checking: if an earlier meeting ran into meeting i, it would also overlap meeting i − 1 (which starts between them), and that pair was already checked.
  4. No clashes → True.
  5. O(n log n) for the sort.

Visual Walkthrough

Running it on intervals = [[0, 30], [5, 10], [15, 20]]:

Meetings overlapping with a long meeting[0,30]0 – 30[5,10]clash[15,20]clash051015202530
After sorting by start, it's enough to compare each meeting with the one just before it. [5, 10] starts at 5, before [0, 30] ends at 30, so one person can't attend both → False.
long meetingoverlaps

Video Explanation

NeetCode — LeetCode 252