Meeting Rooms
Difficulty: Easy
You're given a list of meeting time intervals for one person's calendar, each with a start and end time. Determine whether this person could realistically attend every single meeting — that is, whether any two meetings overlap.
Meetings that just touch (one ends exactly when the next begins) are fine — back-to-back meetings don't count as a conflict.
Examples
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
The meeting [0,30] overlaps both [5,10] and [15,20], so this person can't attend all of them.
Input: intervals = [[7,10],[2,4]]
Output: true
[2,4] finishes before [7,10] starts, so there's no conflict.
Input: intervals = [[1,5],[5,8]]
Output: true
These meetings touch at time 5 but don't overlap, so back-to-back is fine.
Constraints
0 <= intervals.length <= 10^4
intervals[i].length == 2
0 <= start < end <= 10^6
Approach
The direct approach is to compare every pair of meetings and check if they overlap in time — if any pair does, the person can't attend everything.
A faster approach sorts the meetings by start time first. Once sorted, if any two meetings are going to conflict, they must be neighbors in that sorted order — a meeting can't skip over another one to conflict with something further away. So a single pass comparing each meeting to the one right before it is enough to catch every possible conflict.
Solutions
Brute Force — Compare Every Pair
For every pair of meetings, check whether they overlap. Two intervals [a, b] and [c, d] overlap (not just touch) exactly when a < d and c < b. If any pair overlaps, the person can't make every meeting.
function canAttendMeetings(intervals) {
for (let i = 0; i < intervals.length; i++) {
for (let j = i + 1; j < intervals.length; j++) {
const overlaps = intervals[i][0] < intervals[j][1] && intervals[j][0] < intervals[i][1];
if (overlaps) {
return false;
}
}
}
return true;
}Time: O(n^2) — every pair of meetings is checked · Space: O(1) extra space
Optimal — Sort by Start, Check Neighbors
Sort the meetings by start time. Then check each meeting against the one right before it — if the current meeting starts before the previous one ends, they conflict, so the person can't attend both. If every neighboring pair is conflict-free, the whole schedule is conflict-free.
function canAttendMeetings(intervals) {
const sorted = [...intervals].sort((a, b) => a[0] - b[0]);
for (let i = 1; i < sorted.length; i++) {
if (sorted[i][0] < sorted[i - 1][1]) {
return false;
}
}
return true;
}Time: O(n log n) — dominated by the sort · Space: O(n) for the sorted copy