Problem overview
The method canAttendMeetings receives an int[][] named intervals, where each int[] holds a meeting's start and end time. It returns true if one person can attend every meeting and false if any two overlap. Meetings that only touch, such as [1,2] and [2,3], don't overlap, and with no meetings at all the answer is true.
For [[0,30],[5,10],[15,20]] the answer is false: the meeting [5,10] starts while [0,30] is still going. For [[7,10],[2,4]] it is true, even though the meetings are not in order.
The statement asks for O(n log n) time, where n is the number of meetings.
Approach: sort by start, then compare neighbors
Comparing every pair of meetings costs O(n²), which is too slow for up to 100,000 meetings. Sorting by start time fixes that. Once the meetings are in order, a clash can only happen between neighbors. Suppose no clash has appeared so far. Then each earlier meeting ended before the next one started, so the meeting just before the current one is the one that ends last. If the current meeting starts before that one ends, they overlap. If it starts at or after that end, it can't overlap any earlier meeting either.
So the work splits into a sort, O(n log n), and one pass over the neighbors, O(n). The sort is the slowest part and sets the total.
Step by step
- Sort
intervalsby start withArrays.sortand a comparator lambda(first, second) -> first[0] - second[0]. A negative result putsfirstbeforesecond, so the earlier start comes first. - Loop
ifrom 1 whilei < intervals.length. The first meeting has nothing before it to clash with. - Compare with the meeting before. Test
intervals[i][0] < intervals[i - 1][1]: does meetingistart before meetingi - 1ends? Use<, not<=. A meeting that starts exactly when the previous one ends only touches it. - Return
falseinside theif. One clash settles the answer, so the rest don't matter. - Return
trueafter the loop. No meeting started before the one before it ended. With zero or one meeting the loop never runs, andtrueis correct.
The solution in Java
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (first, second) -> first[0] - second[0]);
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}
}
Java details worth noticing:
- Sorting an
int[][].Arrays.sort(array, comparator)works on arrays of objects, and eachint[]is an object, so a comparator can order the rows. A singleint[]of primitives can't take one.Arrayslives injava.util, so outside the lesson's editor the file needsimport java.util.Arrays;. - The sort happens in place. It reorders the array the caller passed in. That is fine here, but a caller who needs the original order must pass a copy.
- Subtraction as a comparator can overflow. The lesson's values stay within 0 to 1,000,000, so
first[0] - second[0]is always safe. With huge values it is not: for starts of 2,000,000,000 and -2,000,000,000 the difference wraps to -294967296, the comparator claims the larger value is smaller, and the sorted result stays[[2000000000, 2], [-2000000000, 1]].Integer.compare(first[0], second[0])has no such problem. - Lambda parameter names.
firstandsecondare the two rows being compared. Their types are inferred from the array's element type.
Complexity
Time: O(n log n). Sorting dominates, and the loop that follows is a single pass of O(n).
Space: the loop uses no extra memory. The sort itself needs some working space for the objects it orders, typically up to O(n), so say O(n) in the worst case rather than O(1).
Edge cases and common mistakes
- No meetings or one meeting.
[]and[[1,5]]both returntrue. - Touching meetings.
[[1,2],[2,3],[3,4]]returnstrue. Writing<=in the comparison would wrongly returnfalse. - Unsorted input.
[[7,10],[2,4]]returnstrueonly because of the sort. Comparing neighbors without sorting would see a start of 2 before an end of 10 and wrongly sayfalse. - A long meeting around short ones. For
[[1,10],[2,3],[11,12]]the method returnsfalsewhen it reaches[2,3], which starts before[1,10]ends. - Comparing with
intervals[0]each time. After sorting, only the meeting just before matters.
Testing
[[0,30],[5,10],[15,20]]givesfalse.[[7,10],[2,4]]givestrue.[[1,2],[2,3],[3,4]]givestrue.[]givestrue.[[1,5]]givestrue.[[1,10],[2,3],[11,12]]givesfalse.
