GeeksforGeeks Medium

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.

Constraints
  • 1 <= start.size() = finish.size() <= 2 * 10⁵
  • 0 <= start[i] <= finish[i] <= 10⁹
  • The next meeting must start strictly after the previous one finishes
greedysortingintervals
Open on GeeksforGeeks ↗
02

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.
How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def activitySelection(self, start, finish):
▶3 meetings = sorted(zip(finish, start))
▶4 count, free_at = 0, -1
▶5 for f, s in meetings:
▶6 if s > free_at:
▶7 count += 1
▶8 free_at = f
▶9 return count
meetings as given, by index0123456789#01–2#13–4#20–6#35–7#48–9#55–9the roomemptyno check yetcount0sort the meetings by end time
n6meeting requests
roomoneone meeting at a time
The question. Each bar is a meeting on the shared time line. Pick as many as possible so no two share a moment. Starting with the earliest start (#2, 0–6) is tempting, but once a meeting is chosen the only thing that matters for the rest is when it ends. So sort by end time.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the roomfree from the startno check yetcount0room is free, count 0
order#0 #1 #2 #3 #5 #4earliest end first
count0
free_at-1below every start
Rows are now ordered by end time, so reading top to bottom always offers the meeting that frees the room soonest. free_at = -1 is below every start time, so the first meeting is guaranteed to fit.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–2free after 21start-1room free after>count1#0 starts at 1, room is empty → take
meeting#0 (1–2)next to end
count1
free_at2was -1
#0 starts at 1 and the room is still empty, so it fits. Of everything that fits it ends earliest, so taking it leaves the most time for the rest. The room is now busy until 2.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–23–4free after 43start2room free after>count2#1 starts at 3, after 2 → take
meeting#1 (3–4)next to end
count2
free_at4was 2
#1 starts at 3, after the room is free (2), so it fits. Of everything that fits it ends earliest, so taking it leaves the most time for the rest. The room is now busy until 4.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–23–4free after 40start4room free after≤count2#2 starts at 0, room busy until 4 → skip
meeting#2 (0–6)
start0before free_at
count2unchanged
#2 starts at 0, while the room is busy until 4. Skip it, and never revisit: free_at only grows, so it would clash with any later choice too.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–23–45–7free after 75start4room free after>count3#3 starts at 5, after 4 → take
meeting#3 (5–7)next to end
count3
free_at7was 4
#3 starts at 5, after the room is free (4), so it fits. Of everything that fits it ends earliest, so taking it leaves the most time for the rest. The room is now busy until 7.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–23–45–7free after 75start7room free after≤count3#5 starts at 5, room busy until 7 → skip
meeting#5 (5–9)
start5before free_at
count3unchanged
#5 starts at 5, while the room is busy until 7. Skip it, and never revisit: free_at only grows, so it would clash with any later choice too.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–23–45–78–9free after 98start7room free after>count4#4 starts at 8, after 7 → take
meeting#4 (8–9)next to end
count4
free_at9was 7
#4 starts at 8, after the room is free (7), so it fits. Of everything that fits it ends earliest, so taking it leaves the most time for the rest. The room is now busy until 9.
sorted by end time0123456789#01–2#13–4#20–6#35–7#55–9#48–9the room1–23–45–78–9free after 9sweep donecount4answer: 4 meetings
taken#0 #1 #3 #4
result4
Answer: 4. The room track holds #0 #1 #3 #4, none overlapping. Each was the earliest-ending meeting still possible, and the exchange argument says no other choice could leave more room, so no schedule beats this count. On this input earliest-start-first gets only 2.
05

Common pitfalls

Sorting by start time

✗ Wrong
meetings = sorted(zip(start, finish))
✓ Right
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

✗ Wrong
if s >= free_at:
✓ Right
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

✗ Wrong
finish.sort()
start.sort()
✓ Right
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.

06

Edge cases

Meetings with the same finish time, e.g. (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.

A meeting that starts and finishes at the same moment, e.g. (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.

07

Complexity

Time
O(n log n)
Space
O(n)
Sorting dominates; the sweep is O(n). The (finish, start) pairs take O(n) extra space, needed because the input arrives as two separate arrays.
08

Similar interval problems

This problem is not on LeetCode, but several problems there read alike and ask something different.

ProblemQuestionMethod
N Meetings in One Room (GFG)most meetings one room can holdsort by finish, greedy sweep
Non-overlapping Intervals (LeetCode 435)fewest intervals to remove so none overlapsame 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 meetingmin-heap of finish times
Minimum Platforms (GFG)fewest platforms for all trainssort arrivals and departures, two pointers
09

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.