N Meetings in One Room
N Meetings in One Room is a GFG problem (Medium). There is one room and n meeting requests: meeting i starts at start[i] and finishes at finish[i]. It is now listed there under the name Activity Selection.
- The room holds one meeting at a time, and a meeting is held in full or not at all.
- The next meeting must start strictly after the previous one finishes, so a meeting that starts at the exact moment another finishes cannot follow it.
Return the maximum meetings in one room that can be held. With up to 2 × 10⁵ requests, trying subsets is out of the question; the budget is one sort and one pass.
- 1 <= start.size() = finish.size() <= 2 * 10⁵
- 0 <= start[i] <= finish[i] <= 10⁹
- The next meeting must start strictly after the previous one finishes
Intuition
Once a meeting is held, all it leaves behind for the rest of the day is the moment the room frees up. So the best first pick is the meeting that finishes earliest, and the same rule repeats on whatever starts after it.
- Why it is safe: take any best schedule and swap its first meeting for the earliest-finishing one. The room frees up no later, so every later meeting still fits and the count is unchanged. Repeat the swap on the rest.
- Why other rules fail: shortest-first is tempting, but on
(1, 5),(4, 7),(6, 10)the short(4, 7)clashes with both others and gives 1. Finishing first takes(1, 5)and(6, 10): 2.
"Hold the most non-overlapping intervals" is the activity selection problem: sort by finish time, remember the last finish, take what fits. Non-overlapping Intervals (LeetCode 435) is the same sweep with the answer n minus this count, and Minimum Number of Arrows to Burst Balloons (LeetCode 452) counts the same groups.
Approach
Before reading on, find three meetings where taking the shortest one first gives a worse answer than taking the one that finishes first. Then say which single comparison decides whether a meeting is taken.
Pair and sort by finish time
Zip each meeting into a (finish, start) pair and sort. Ties on the finish time can go in any order: two meetings that finish together free the room at the same moment, so the rest of the sweep sees the same state either way.
Keep one number: when the room is free
Set free_at = -1, below every start time, so the first meeting always fits, and count = 0. Nothing else about the meetings already taken is needed again.
Take each meeting that starts after the room frees
If start > free_at, take the meeting: add 1 to count and set free_at to its finish. Otherwise skip it for good, because free_at only grows, so a meeting that clashes now would clash later too.
Code in Python, C++ and Java
free_at = -1 is below every start time, so the first meeting is guaranteed to fit.free_at only grows, so it would clash with any later choice too.free_at only grows, so it would clash with any later choice too.free_at = -1 is below every start time, so the first meeting is guaranteed to fit.free_at only grows, so it would clash with any later choice too.>= check would wrongly take it.free_at = -1 is below every start time, so the first meeting is guaranteed to fit.free_at only grows, so it would clash with any later choice too.Common pitfalls
Sorting by start time
meetings = sorted(zip(start, finish))
meetings = sorted(zip(finish, start))
Sorting by start lets one long early meeting claim the room first. With meetings (0, 10), (1, 2), (3, 4), (5, 6) it takes (0, 10) and returns 1 instead of 3.
Allowing back-to-back meetings
if s >= free_at:
if s > free_at:
Here a meeting may not start at the instant the previous one finishes. With (10, 20) and (20, 30) the >= version returns 2; the expected answer is 1. Many interval problems elsewhere allow touching, so check which rule the statement uses.
Sorting the two arrays separately
finish.sort() start.sort()
meetings = sorted(zip(finish, start))
Sorting each array on its own breaks the pairing, so the sweep sees meetings that do not exist. On start = [1, 1, 3, 5], finish = [2, 5, 4, 6] the real meetings (1, 2), (3, 4), (5, 6) give 3, but after separate sorts (3, 4) becomes (3, 5) and the answer drops to 2.
Edge cases
(1, 5) and (3, 5)Both contain the moment 5, so only one can be held, and it does not matter which. The sweep takes the first, and the second fails start > free_at.
(3, 3) and (3, 4)The constraints allow start[i] == finish[i]. The zero-length meeting is taken and sets free_at = 3, so (3, 4) cannot follow it; the answer is 1.
Complexity
(finish, start) pairs take O(n) extra space, needed because the input arrives as two separate arrays.Similar interval problems
This problem is not on LeetCode, but several problems there read alike and ask something different.
| Problem | Question | Method |
|---|---|---|
| N Meetings in One Room (GFG) | most meetings one room can hold | sort by finish, greedy sweep |
| Non-overlapping Intervals (LeetCode 435) | fewest intervals to remove so none overlap | same sweep; answer is n minus the count |
| Meeting Rooms (LeetCode 252) | can one person attend all meetings? | sort by start, check neighbours |
| Meeting Rooms II (LeetCode 253) | fewest rooms to hold every meeting | min-heap of finish times |
| Minimum Platforms (GFG) | fewest platforms for all trains | sort arrivals and departures, two pointers |
N Meetings in One Room FAQ
How do I list which meetings were chosen, not just how many?
Sort triples (finish, start, index) instead of pairs, and append index + 1 to a list each time a meeting is taken. The older version of this problem asked for that list in the order the meetings are held; the current one asks only for the count.
Is this the same as the activity selection problem?
Yes. Activity selection is the textbook name for choosing the most non-overlapping activities, and the earliest-finish greedy rule is its standard solution. Here the activities are meetings, and the strict rule means two activities may not share an endpoint.