Intervals: Meeting Rooms

Interval problems get easy once the intervals are sorted. Then you sweep from left to right and only compare each interval with what came just before. Sorting is O(n log n), and the sweep is O(n).

The signal: start and end times, bookings, ranges that may overlap.

To count the rooms needed, turn each meeting into two events: (start, +1) and (end, -1). Sort the events and keep a running count of meetings in progress. The answer is the highest count. A meeting that ends at 10 frees its room for one that starts at 10. Because -1 sorts before +1, ties come out right for free.

A common mistake: comparing every meeting with every other meeting. That is O(n²), and it miscounts overlaps that chain together.

The Algorithms challenge "merge bookings" uses the same sort and sweep.

Exercise: finish rooms_needed so it returns the smallest number of rooms that fits all meetings, given as (start, end) pairs. Press Check to run the tests.