Meeting Rooms

EasyIntervals · Sorting🧩 Intervals & Line SweepLeetCode #252 ↗Updated

Problem

You are given meeting times intervals[i] = [start, end], where a meeting occupies the half-open range [start, end). Return true if one person could attend all of them, i.e. no two meetings overlap. A meeting that ends at time t does not clash with one that starts at t.
Inputintervals = [[0,30],[5,10],[15,20]]Outputfalse
Inputintervals = [[7,10],[2,4]]Outputtrue
Inputintervals = [[1,5],[5,8]]Outputtrue

0 ≤ intervals.length ≤ 104  ·  0 ≤ start < end ≤ 106

Try it yourself

Java
class Solution {
    public boolean canAttendMeetings(int[][] intervals) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
Once meetings are sorted by start time, any overlap must show up between neighbours: if meeting i clears meeting i-1, it clears every earlier one too.
PatternIntervals & Line Sweep

Instinct See [start, end] pairs, meetings, bookings or the word "overlap"? Sort by an endpoint, then sweep left to right carrying one running fact.

Memory hack Sort, then sweep - start time to merge, end time to choose, +1/-1 events to count the crowd.

Open the Intervals & Line Sweep guide →

Approaches

Brute ForceCheck Every Pair

Time O(n2)Space O(1)

Two half-open ranges [a,b) and [c,d) overlap exactly when a < d and c < b. Test that condition for every pair; any hit means the schedule is impossible.

Java
public boolean canAttendMeetings(int[][] intervals) {
    int n = intervals.length;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            boolean overlap = intervals[i][0] < intervals[j][1]
                    && intervals[j][0] < intervals[i][1];
            if (overlap) return false;
        }
    }
    return true;
}

Direct and correct, but quadratic for 104 meetings.

At a glance

LevelApproachTimeSpace
Brute ForceCheck Every PairO(n2)O(1)
OptimalSort by Start, Compare NeighboursO(n log n)O(log n)
Common pitfalls
  • Using <= — back-to-back meetings like [1,5],[5,8] are fine.
  • Comparing only with the first meeting instead of the previous one.
  • Forgetting the empty and single-meeting cases (both true).
Interview tips
  • Confirm whether intervals are half-open or closed before coding.
  • Explain why adjacent checks suffice after sorting.
  • Expect the follow-up: how many rooms are needed (Meeting Rooms II).
Pattern
Sort intervals by start → overlap questions reduce to comparing neighbours.
My notes saved in this browser