Meeting Rooms
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
Given an array of meeting time intervals, where intervals[i] = [start, end], return true if one person could attend all the meetings, meaning no two of them overlap.
Understand the problem
- An interval
[start, end]is a stretch of time fromstartup toend. - Two meetings overlap if one starts before the other has ended.
- Meetings that only touch, like
[1, 2]and[2, 3], do not overlap: you can walk straight from one to the next. - The meetings are not given in time order.
Example 1
Input: intervals = [[0, 30], [5, 10], [15, 20]]
Output: false[5, 10] starts while [0, 30] is still going.
Example 2
Input: intervals = [[7, 10], [2, 4]]
Output: trueIn time order: [2, 4], then [7, 10]. No clash.
Example 3
Input: intervals = []
Output: trueNo meetings, nothing to clash.
Constraints
0 <= intervals.length <= 10^40 <= start < end <= 10^6
Hints
Stuck? Open one hint at a time. Each one gives away a little more than the last.
The idea in one sentence
Sort the meetings by start time, then check that each one starts no earlier than the previous one ends.
The naive approach (and why it's slow)
Compare every pair of meetings: does one start before the other ends? That's about n²/2 comparisons, O(n²).
The key insight: sort by start
This is the first move in almost every interval problem. Once the meetings are in time order, a meeting can only clash with its direct neighbor before it.
Why? Picture a timeline, and say B comes right after A (sorted by start). If some earlier meeting reached into B, it would still be running when B starts, which is at or after A's start. So it would already overlap A, and we'd have caught that clash first.
So:
- Sort by
start. - For each meeting after the first: if
start < previous end, returnfalse. - If you get through them all, return
true.
Step by step
intervals = [[0, 30], [5, 10], [15, 20]]. Sorted by start: the same order.
time: 0 5 10 15 20 30
[==================================] [0, 30]
[====] [5, 10]
[====] [15, 20]| Meeting | Previous ends at | Starts before that? | Result |
|---|---|---|---|
[0, 30] | (first) | ||
[5, 10] | 30 | 5 < 30: yes | return false |
The code
The reference solution is below, with comments on every important line.
How fast is it?
- Time: O(n log n) for the sort. The check afterward is O(n).
- Memory: O(n) for the sorted copy.
Common mistakes
- Using
<=and treating touching meetings ([1, 2],[2, 3]) as a clash. - Forgetting to sort. Without sorting, comparing neighbors misses clashes between meetings that are far apart in the input.
- Sorting by end here. It also works for this problem, but sorting by start is the habit to build.
Going deeper
- Meeting Rooms II asks how many rooms you'd need if meetings can overlap.
- "Sort, then compare each interval with the previous one" is the backbone of Merge Intervals too.
Reference solution
/**
* @param {number[][]} intervals
* @return {boolean}
*/
export default function canAttendMeetings(intervals) {
// In start order, a meeting can only clash with the one right before it.
const sorted = [...intervals].sort((a, b) => a[0] - b[0])
for (let i = 1; i < sorted.length; i++) {
// Starts before the previous one ends? (Touching, start === end, is fine.)
if (sorted[i][0] < sorted[i - 1][1]) return false
}
return true
}export default function canAttendMeetings(intervals: number[][]): boolean {
// In start order, a meeting can only clash with the one right before it.
const sorted = [...intervals].sort((a, b) => a[0] - b[0])
for (let i = 1; i < sorted.length; i++) {
// Starts before the previous one ends? (Touching, start === end, is fine.)
if (sorted[i][0] < sorted[i - 1][1]) return false
}
return true
}def can_attend_meetings(intervals: list[list[int]]) -> bool:
# In start order, a meeting can only clash with the one right before it.
intervals = sorted(intervals)
# Each meeting must start no earlier than the previous one ends (touching is fine).
return all(intervals[i][0] >= intervals[i - 1][1] for i in range(1, len(intervals)))