Overview
Given an array of meeting time intervals where intervals[i] = [start, end], determine if a person could attend all meetings (i.e., no two meetings overlap).
Implement canAttendAll(intervals).
Constraints
- Each interval is a two-element array
[start, end]wherestart < end. - Return
trueif there are no overlapping meetings,falseotherwise. - Input may be unsorted.
- Attend means the person must be present for the entire duration. Adjacent meetings (
[0,5],[5,10]) do NOT conflict. - Aim for $O(n \log n)$ time.
Examples
canAttendAll([[0,30],[5,10],[15,20]]); // → false — [0,30] overlaps with both others canAttendAll([[7,10],[2,4]]); // → true — no overlap canAttendAll([[1,5],[5,10]]); // → true — adjacent, not overlapping
Notes
- Sort by start time and compare consecutive pairs.
- If any meeting starts before the previous one ends, return false.
Solution
Reveal solution
function canAttendAll(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}Resources
meeting-calendar.js
Meeting Calendar
hardcodingAlgorithmsSorting
Overview
Given an array of meeting time intervals where intervals[i] = [start, end], determine if a person could attend all meetings (i.e., no two meetings overlap).
Implement canAttendAll(intervals).
Constraints
- Each interval is a two-element array
[start, end]wherestart < end. - Return
trueif there are no overlapping meetings,falseotherwise. - Input may be unsorted.
- Attend means the person must be present for the entire duration. Adjacent meetings (
[0,5],[5,10]) do NOT conflict. - Aim for $O(n \log n)$ time.
Examples
canAttendAll([[0,30],[5,10],[15,20]]); // → false — [0,30] overlaps with both others canAttendAll([[7,10],[2,4]]); // → true — no overlap canAttendAll([[1,5],[5,10]]); // → true — adjacent, not overlapping
Notes
- Sort by start time and compare consecutive pairs.
- If any meeting starts before the previous one ends, return false.
Solution
Reveal solution
function canAttendAll(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}Resources
NameTopicDifficulty
103 of 103 problems