2Brute2Best
🔥0

Meeting Rooms

Easy·LeetCode #252 PremiumBasic Anchor
LeetCode
30-Second Mental ModelIntervals — Scheduling (sort + neighbour check)
Complexity:O(N²)→O(N log N)
Key Insight

Sort the meetings by start time. Then walk through them and check one thing: does this meeting start before the previous meeting ends? If yes, they overlap. If that never happens, there is no clash anywhere.

Core Validity Invariant

Meeting i is fine when it starts at or after the previous meeting ends: intervals[i][0] ≥ intervals[i−1][1]. Equal is fine, because a meeting is over at its end time.

01

Problem

You get a list of meetings. Meeting i runs from intervals[i][0] to intervals[i][1].

One person wants to go to every meeting. Return true if no two meetings share any time, and false otherwise.

A meeting is over at its end time. So a meeting that ends at 10 and one that starts at 10 do not clash: you walk out of one and into the other.

Input

A list intervals of [start, end] pairs.

Output

true if one person can attend every meeting, false if any two of them overlap.

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

[0,30] is still running when [5,10] starts at 5, so you can't be in both.

Example 2
Input: intervals = [[7,10],[2,4]]
Output: true

[2,4] ends at 4, long before [7,10] starts.

Example 3
Input: intervals = [[5,10],[10,15]]
Output: true

The first meeting is over at 10, exactly when the second one starts. Touching is not overlapping.

Constraints
  • • 0 ≤ intervals.length ≤ 10^4
  • • intervals[i].length == 2
  • • 0 ≤ start < end ≤ 10^6
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    When exactly do two meetings clash? Write the test for two meetings first.

  2. 2.

    Is a meeting ending at 10 in conflict with one starting at 10?

  3. 3.

    If I had to check meetings in some order, which order would put clashing meetings next to each other?

  4. 4.

    After sorting, do I need to compare a meeting with everything before it, or only with one of them?

  5. 5.

    What should the answer be for zero meetings or one meeting?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceCheck all n(n−1)/2 pairs of meetings for overlapO(N²)O(1)
OptimalSort by start; each meeting only needs checking against the previous oneO(N log N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “list of [start, end] meetings”Interval input → sort first, then walk once.
  • “attend all”Yes/no question: does ANY overlap exist? We can stop at the first one.
  • “one person”At most 1 meeting may be active at any time. Meeting Rooms II asks how many are active at once.
Intervals+sort by start+compare with the previous one→Intervals — Scheduling (sort + neighbour check)

This is the anchor of the scheduling sub-pattern. The question is "is the number of meetings running at once ever more than 1?". With one person, sorting and a neighbour check answer it. Meeting Rooms II asks for the maximum number running at once, which needs a sweep or a heap. Merge Intervals uses the same sort but combines overlapping meetings instead of rejecting them.

10

Don't make this mistake

Treating touching meetings as overlapping
✗ if (intervals[i][0] <= intervals[i - 1][1]) return false;
✓ if (intervals[i][0] < intervals[i - 1][1]) return false;

[5,10] and [10,15] do not overlap: the first meeting is over at 10, and the second starts at 10. With <= you would wrongly return false.

Forgetting to sort

Without sorting, the meeting just before i in the list is not the one just before it in time. [[10,20],[0,5]] has no clash at all, but the unsorted check sees 0 < 20 and wrongly returns false. Always sort first.

Wrong overlap test in the brute force
✗ if (s1 <= s2 && s2 < e1) return false; // only checks one direction
✓ if (s1 < e2 && s2 < e1) return false;

The one-sided test only catches meeting 2 starting inside meeting 1. It misses meeting 1 starting inside meeting 2. The symmetric test catches every case, including one meeting inside the other.

Merging instead of checking (pattern confusion)

This looks like Merge Intervals, so it is tempting to merge everything and compare the counts. That works, but it is extra work and extra space. Here you only need to find one clash: return false at the first one. Also, Merge Intervals treats touching intervals as overlapping ([1,4] and [4,5] merge), while here touching meetings are fine.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.