Problem overview
The method canAttendMeetings receives intervals, an array where each meeting is a pair [start, end]. It returns true if one person could attend every meeting, and false if any two of them overlap. Two meetings overlap when one starts while the other is still going. Meetings that only touch, like [1,2] and [2,3], don't overlap, because the person can leave one at time 2 and enter the next at time 2. With no meetings, the answer is true.
For [[0,30],[5,10],[15,20]] the answer is false, since [5,10] starts at 5 while [0,30] is running. For [[7,10],[2,4]] it is true, even though the input isn't in order. For [[1,2],[2,3],[3,4]] it is true: each meeting starts exactly when the previous one ends. The lesson asks for O(n log n) time.
Approach: sort, then compare neighbors
Comparing every pair of meetings takes O(n²) time, which is too slow for 100,000 meetings. Sorting by start time makes the problem local: in sorted order, a clash can only happen between two neighbors.
The reason is that if no clash has appeared so far, each earlier meeting ended before the next one started. So the meeting just before the current one is the one that ends last, and it is the only one worth comparing with. After sorting, one pass is enough: meeting i clashes with the meeting before it exactly when it starts before that meeting ends.
The sort costs O(n log n) and is the slowest part. The pass after it costs O(n).
Step by step
- Sort
intervalsby start. Callsortwith a compare function(first, second) => first[0] - second[0]. A negative result placesfirstbeforesecond, so the earlier start comes first. - Loop
ifrom 1 whilei < intervals.length. The first meeting has nothing before it to clash with, so the loop starts at the second. - Test
intervals[i][0] < intervals[i - 1][1]: does meetingistart before the previous one ends? Use<and not<=, because a meeting that starts exactly when the previous one ends only touches it. - Return
falseinside thatif. One clash settles the answer, so the rest doesn't matter. - Return
trueafter the loop. No meeting started before its predecessor ended. With zero or one meeting the loop doesn't run, and the answer istrueas well.
The solution in JavaScript
class Solution {
canAttendMeetings(intervals) {
intervals.sort((first, second) => first[0] - second[0]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}
}
The compare function is the important JavaScript detail. Array.prototype.sort() with no compare function converts the elements to strings and sorts them as text. For plain numbers, [10, 9, 1].sort() gives [1, 10, 9]. For pairs it is just as wrong, because each pair becomes a string such as "10,12":
const meetings = [[2, 4], [10, 12], [9, 10]];
console.log(JSON.stringify(meetings.sort()));
// [[10,12],[2,4],[9,10]]
console.log(JSON.stringify(meetings.sort((a, b) => a[0] - b[0])));
// [[2,4],[9,10],[10,12]]
Subtracting the starts is a safe compare here, because the values are small non-negative integers. Note also that sort works in place: after the call, the caller's intervals array is reordered. The lesson's method does that on purpose, but it is something to remember if the original order matters elsewhere. For [[7,10],[2,4]], the array holds [[2,4],[7,10]] afterwards.
Complexity
Sorting takes O(n log n) time and the loop takes O(n), so the total is O(n log n). Beyond the sort's own working memory, the method stores only the loop index.
Edge cases and common mistakes
- Empty input or one meeting.
[]and[[3,4]]both returntrue, because the loop has nothing to compare. - Touching meetings.
[[1,2],[2,3],[3,4]]returnstrue. Using<=would wrongly reject it. - Forgetting to sort. The same neighbor check on the unsorted
[[7,10],[2,4]]compares 2 with 10 and returnsfalse, although the meetings never overlap. The check only means something once the meetings are in order of start. - Sorting without a compare function. It orders the pairs as text, which can put
[10,12]before[2,4]. - A long meeting hiding smaller ones. In
[[1,5],[2,3],[6,7]]the clash is found at[2,3], which starts before 5, so the answer isfalse.
Testing
Results from the reference solution:
[[0,30],[5,10],[15,20]] -> false
[[7,10],[2,4]] -> true
[[1,2],[2,3],[3,4]] -> true
[] -> true
[[3,4]] -> true
[[1,5],[2,3],[6,7]] -> false
