Picking the most activities
🌍 In real life. A club has one meeting room, and eight groups ask to use it tomorrow, each for a set time. Two meetings can't share the room, so the secretary must turn some away. To make as many groups happy as possible, which meetings should be booked?
The idea in one sentence: sort the meetings by when they end, then book each one that starts once the last booked one has ended; finishing early always leaves the most room for what comes after.
Its words:
- activity selection: choosing the most intervals with no two overlapping
- compatible: two meetings that don't overlap (one may start exactly when the other ends)
- end time: what greedy sorts by here
Step by step:
- sort the meetings by their end time
- book the first one
- go down the list: book a meeting if it starts at or after the end of the last one booked, else skip it
Watch it on the club's day. Three meetings fit, and no choice fits four:
Why ending earliest is safe (an exchange argument). Take any best schedule and look at its first meeting. The meeting that ends earliest of all ends no later than it, so swap it in: it can't clash with the rest, which all start after that first meeting ended. The schedule is just as long and now starts with greedy's choice. Repeat the argument on what is left, and greedy's schedule is as long as the best.
More than one room. A second question: if every group must get a room, how many rooms are needed? Sort by start, and keep a heap of when each room comes free; heapq.heapreplace(ends, e) pops the earliest end and pushes e in one call. A meeting takes the room that frees up first if that room is free by its start, otherwise it opens a new room:
What it costs, by counting. Both are a sort, O(n log n), then one pass: O(n) for the booking, and O(n log n) for the rooms, since each meeting does at most one heap operation. For 100,000 meeting requests that is about 100,000 × 17 = 1,700,000 comparisons for the sort. Trying every subset of meetings instead would mean 2 to the power 100,000 subsets.
def max_meetings(meetings):
count, last_end = 0, None
for s, e in sorted(meetings, key=lambda m: m[1]): # the one that ends earliest first
if last_end is None or s >= last_end: # it starts once the last one has ended
count += 1
last_end = e
return count
import heapq
def min_rooms(meetings):
ends = [] # when each room's last meeting ends (a min-heap)
for s, e in sorted(meetings): # by start
if ends and ends[0] <= s: # the room that frees up first is free in time
heapq.heapreplace(ends, e)
else:
heapq.heappush(ends, e) # every room is busy: open one more
return len(ends)
day = [(1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11)]
print(max_meetings(day))
print(min_rooms([(9, 10), (9, 12), (10, 11), (11, 13), (12, 14)]))Mistakes and edge cases.
- sorting by start or by length: both have counterexamples (one long early meeting; a short meeting that overlaps two others)
- touching meetings: one ending at 3 and another starting at 3 don't clash; use
s >= last_end, and in the rooms count,ends[0] <= s - no meetings: zero booked, zero rooms
- two different questions: the most meetings in one room (sort by end) is not the fewest rooms for all (sort by start)
Quiz
To fit the most meetings in one room, sort them by…
- when they end
- when they start
- how long they last
What does this print?
def max_meetings(meetings): count, last_end = 0, None for s, e in sorted(meetings, key=lambda m: m[1]): # the one that ends earliest first if last_end is None or s >= last_end: # it starts once the last one has ended count += 1 last_end = e return count print(max_meetings([(1, 3), (3, 5), (2, 4), (4, 6)]))Meetings (1, 4), (2, 5) and (4, 6): how many rooms are needed?
- 2: (4, 6) uses the room (1, 4) has left
- 3
- 1
Why may greedy swap the earliest-ending meeting into any best schedule?
- It ends no later than that schedule's first meeting, so it clashes with nothing after
- It is always the shortest meeting
- It starts first
This sometimes books fewer meetings than possible. Which line is wrong?
def max_meetings(meetings): count, last_end = 0, None for s, e in sorted(meetings): if last_end is None or s >= last_end: count += 1 last_end = e return count
Coding tasks
Write
max_meetings(meetings):meetingsis a list of(start, end)pairs for one room. Return the most meetings that can be booked with no two overlapping (a meeting may start exactly when another ends).Write
min_rooms(meetings): the fewest rooms so that every meeting gets one and none has to wait. A room is free again exactly when its meeting ends.