Blind 75 · Intervals

Meeting Rooms

Easy

Watch the walkthrough (5:13)

Problem

Given a list of meeting time intervals, decide whether one person can attend them all — that is, whether no two meetings overlap.
A meeting may start the minute another ends.

Examples

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

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

Example 3:
Input: intervals = [[1,5],[5,8]]
Output: true
Explanation: back to back is attendable.

Example 4:
Input: intervals = [[6,7],[2,4],[8,12]]
Output: true

Constraints

1 <= intervals.length <= 10^4
intervals[i] is [start, end] with 0 <= start < end <= 10^6

Prerequisites

Sorting as a setup move — most interval problems are one sort away from a single sweep, and choosing whether to sort by START or by END is the decision that matters.

How to think about it

1. Sort by Start, Catch Any Clash Optimal

Intuition

Sorted by start, if any two meetings overlap, some meeting overlaps the meeting just before it: the first clash is always between neighbours. So after the sort one pass settles it: check each meeting against the one just before it, and if a meeting starts before the last one ends, the two overlap and the answer is false; if none does, it is true. A meeting may start the minute another ends, so the check is strictly less than: touching is fine. Touching is not a clash. If the check were less than or equal, the walkthrough's 4..7, which starts where 2..4 ends, would count as a clash, and the very first check would return false; that is wrong, because a meeting may start the minute another ends. Sort by start; if a meeting starts before the last ends, return false.

Algorithm

1. Sort the meetings by start.
2. For each meeting from the second on, if it starts before the meeting just before it ends, return false.
3. If none does, return true.

Time & Space

Time O(n log n) for the sort; the pass after it is O(n). Space O(1) besides the sort.

2. Neighbours After Sorting Optimal

Intuition

Sorted by start, if any two meetings overlap, some meeting overlaps the one just before it, so the first clash is always between neighbours. One pass over neighbours settles it, and back-to-back meetings are fine, so the comparison is strict.

Algorithm

1. Sort by start.
2. If any meeting starts strictly before the previous one ends, return false.
3. Otherwise all meetings are attendable.

Time & Space

Time O(n log n). Space O(1).