Visualize

Pattern visualizer

Meeting Rooms

Unsorted, any pair could be the clash, which means checking all n^2 of them. Sorting by start time removes that: once the meetings are in time order, a meeting can only ever collide with the one immediately before it, because everything earlier ended even sooner. So one sweep over adjacent pairs decides it. A meeting that ends exactly when the next begins does NOT count as a clash — the test is strictly previous end > current start. Animated on: meetings = [[19,22],[4,8],[25,28],[0,3],[20,23],[13,15],[30,33],[8,11]] — can one person attend every meeting, or do two of them overlap?.

Sort by start, then check neighbours

time O(n log n)space O(1)step 1 / 8
[19,22]
[0]
[4,8]
[1]
[25,28]
[2]
[0,3]
[3]
[20,23]
[4]
[13,15]
[5]
[30,33]
[6]
[8,11]
[7]
line 1

8 meetings arrive in booking order, not time order: [19,22], [4,8], [25,28], [0,3], [20,23], [13,15], [30,33], [8,11]. In this jumble a clash can sit anywhere, so there is nothing useful to compare yet.

Pseudocode
1FUNCTION canAttendAll(meetings)
2 SORT meetings BY START ASCENDING
3 FOR i <- 1 TO LENGTH(meetings) - 1
4 prevEnd <- END(meetings[i - 1])
5 currStart <- START(meetings[i])
6 IF prevEnd > currStart
7 RETURN false
8 RETURN true

← / → step · space play · Home restart

Where to practice Intervals