AlgoMaster Logo

Meeting Rooms

Last Updated: March 29, 2026

easy

Understanding the Problem

We're given a list of meetings, each with a start and end time, and we need to figure out if any two meetings overlap. If they do, a person can't attend all of them, so we return false. If no meetings overlap, we return true.

Two meetings overlap when one starts before the other ends. Think about it on a timeline: if meeting A runs from 1 to 5 and meeting B runs from 3 to 7, there's a conflict between time 3 and 5. The key observation is that if we sort the meetings by start time, we only need to check consecutive meetings for overlap. Why? Because if meetings are sorted and meeting B doesn't overlap with meeting A, then any meeting starting after B can't overlap with A either.

Key Constraints:

  • 0 <= intervals.length <= 10^4 → With n up to 10,000, an O(n^2) brute force (checking every pair) would mean up to 100 million comparisons. That's borderline but likely too slow. An O(n log n) sorting approach is safe and efficient.
  • 0 <= start_i < end_i <= 10^6 → Start is always strictly less than end, so every meeting has a positive duration. We don't need to handle zero-length meetings or inverted intervals.

Approach 1: Brute Force

Intuition

The most direct approach: compare every pair of meetings and check if they overlap. Two meetings [s1, e1] and [s2, e2] overlap if one starts before the other ends and vice versa. Formally, they overlap if s1 < e2 and s2 < e1.

This is what you'd do if someone handed you a list of meetings on paper. You'd go through each pair one by one and check for conflicts. It works, but it's slow because you're checking n*(n-1)/2 pairs.

Algorithm

  1. For each meeting at index i, compare it with every meeting at index j > i.
  2. Two meetings overlap if intervals[i][0] < intervals[j][1] and intervals[j][0] < intervals[i][1].
  3. If any pair overlaps, return false.
  4. If no overlapping pair is found, return true.

Example Walkthrough

1Start: compare every pair of meetings for overlap
[0, 30]
[5, 10]
[15, 20]
030
1/3

Code

This works but checks many unnecessary pairs. What if we arranged the meetings in chronological order so we only need to check each meeting against its neighbor?

Approach 2: Sorting

Intuition

Here's the key insight: if we sort the meetings by start time, overlapping meetings must be adjacent in the sorted order. Think about why. If meeting A starts at time 2 and meeting C starts at time 10, any meeting B that starts between them (say at time 5) would appear between A and C after sorting. So if A overlaps with C, it definitely overlaps with B too (since B starts earlier than C).

This means after sorting, we only need to check each meeting against the one right before it. If the current meeting starts before the previous one ends, we have an overlap. One pass through the sorted array and we're done.

Algorithm

  1. Sort the intervals by their start time.
  2. Iterate through the sorted intervals starting from index 1.
  3. For each meeting, check if its start time is less than the previous meeting's end time.
  4. If yes, return false (overlap detected).
  5. If we finish the loop without finding any overlap, return true.

Example Walkthrough

1Input: [[0,30],[5,10],[15,20]] (unsorted)
[0, 30]
[5, 10]
[15, 20]
030
1/4

Code