Check whether one person can attend every meeting.
Start with the concrete trace below. It shows the state the algorithm must carry as it runs.
Problem trace
Meeting Rooms: Sorting by start time makes the first overlap appear between adjacent meetings, so Python all can stop at the first failed boundary check.
Input and goalCheck whether one person can attend every meeting.
Start with unsorted meetingsUse intervals [[7,10],[2,4],[12,14],[9,11]]. Their input order does not reveal the chronological neighbor checks.
[7,10]
[2,4]
[12,14]
[9,11]
input[[7,10],[2,4],[12,14],[9,11]]
Sort lexicographicallyintervals.sort() orders starts as [[2,4],[7,10],[9,11],[12,14]]. Equal starts would be ordered by end, but this input has distinct starts.
[2,4]
[7,10]
[9,11]
[12,14]
sorted[[2,4],[7,10],[9,11],[12,14]]
Check boundary at index 1Compare previous end 4 with current start 7. Since 4 <= 7, [2,4] finishes before [7,10] begins, and all continues.
[2,4]
[7,10]
[9,11]
[12,14]
index1boundary4 <= 7: true
Check boundary at index 2Compare previous end 10 with current start 9. The test 10 <= 9 is false, and the bars overlap from time 9 to 10.
[2,4]
[7,10]
[9,11]
[12,14]
index2boundary10 <= 9: falseoverlap[9,10]
Short-circuit and return falsePython all stops after the false index-2 comparison, so index 3 is not evaluated. One person cannot attend [7,10] and [9,11].
[2,4]
[7,10]
[9,11]
[12,14]
skippedindex 3: 11 <= 12 not evaluatedresultfalse
Recognize it
Use it when one resource must handle every interval and any temporal overlap makes the schedule invalid.
Keep true
Before checking index i, all adjacent boundaries before i are non-overlapping. In start-sorted order, any overlap must include an adjacent pair, so the first failed comparison proves impossibility.
Reuse it
Sort to make a global conflict local, then compare neighboring boundaries and stop at the first violation. This transfers to overlap validation, booking conflicts, and disjoint-range checks.
Read it this way: Use intervals [[7,10],[2,4],[12,14],[9,11]]. Their input order does not reveal the chronological neighbor checks. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Pattern: Sort intervals by start.
Simple idea: After sorting, only neighboring meetings can reveal the first overlap. Each
meeting must start at or after the previous meeting ends.
def can_attend_meetings(intervals: list[list[int]]) -> bool: intervals.sort() return all( intervals[index - 1][1] <= intervals[index][0] for index in range(1, len(intervals)) )
Cost:O(nlogn) time and O(1) extra space, not counting sorting.