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
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.
1FUNCTION canAttendAll(meetings)2 SORT meetings BY START ASCENDING3 FOR i <- 1 TO LENGTH(meetings) - 14 prevEnd <- END(meetings[i - 1])5 currStart <- START(meetings[i])6 IF prevEnd > currStart7 RETURN false8 RETURN true
← / → step · space play · Home restart