Problem overview
The method canAttendMeetings receives a list intervals where each meeting is a list [start, end]. It returns True if one person can attend every meeting, and False if any two overlap. Two meetings overlap when one starts while the other is still going. Meetings that only touch, such as [1, 2] and [2, 3], don't overlap, because the person leaves one at time 2 and enters the next at time 2. With no meetings the answer is True. There can be up to 100,000 meetings, and the statement asks for O(n log n) time.
For [[0, 30], [5, 10], [15, 20]] the answer is False: the meeting at 5 starts while the one from 0 to 30 is running. For [[7, 10], [2, 4]] it is True, even though the list isn't in time order.
Approach: sort, then compare neighbours
Comparing every pair of meetings takes O(n²), which is too slow for 100,000 meetings. Sorting by start time removes the need. Once the meetings are in order of start, a clash can only happen between neighbours. If no clash has been found so far, every earlier meeting ended before the next one started, so the meeting just before is the one that ends last. Checking it is enough.
The sort costs O(n log n) and the check is one pass, so the sort is the slowest part.
Step by step
- Sort
intervalsby start withsortand akeygiven by a lambda that returnsinterval[0]. The key tellssortwhat to compare. - Loop
ioverrange(1, len(intervals)), every meeting from the second one on. The first meeting has nothing before it to clash with. - Test
intervals[i][0] < intervals[i - 1][1]: does meetingistart before the previous meeting ends? Use<and not<=: a meeting that starts exactly when the previous one ends only touches it. - Return
Falseinside theif. One clash settles the answer, so the later meetings don't matter. - Return
Trueafter the loop. With zero or one meeting the loop doesn't run, and the answer isTruetoo.
The solution in Python
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
intervals.sort(key=lambda interval: interval[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i - 1][1]:
return False
return True
Python details worth knowing:
sort()works in place and returnsNone. After the call, the caller's list is reordered: sorting[[7, 10], [2, 4]]leaves it as[[2, 4], [7, 10]]. Writingintervals = intervals.sort(...)would makeintervalsNone.sorted(intervals, key=...)returns a new list and leaves the original alone.- The key function. Without
key, Python would compare whole lists, start first and end second. Sorting byinterval[0]alone is enough here, and the sort is stable, so meetings with the same start keep their input order. - A
lambdais a small unnamed function.lambda interval: interval[0]takes a meeting and returns its first number.
Complexity
Time: O(n log n) for the sort, plus O(n) for the loop. Space: the loop uses O(1) extra memory. Python's sort may use up to O(n) temporary space internally.
Edge cases and common mistakes
- No meetings.
[]givesTrue, and one meeting such as[[1, 5]]does too. - Touching meetings.
[[1, 2], [2, 3], [3, 4]]givesTrue. With<=in the test the answer would wrongly beFalse. - A meeting inside another.
[[1, 4], [2, 3]]givesFalse. - Skipping the sort. Comparing neighbours in the original order is wrong. On
[[7, 10], [2, 4]]the test2 < 10is true, so the function would report a clash between two meetings that don't overlap. - A long meeting hiding a clash.
[[1, 10], [2, 3], [11, 12]]givesFalse, found at the pair[1, 10]and[2, 3].
Testing
[[0, 30], [5, 10], [15, 20]]givesFalse.[[7, 10], [2, 4]]givesTrue.[[1, 2], [2, 3], [3, 4]]givesTrue.[]givesTrue.[[1, 4], [2, 3]]givesFalse.[[1, 3], [2, 4]]givesFalse.
