GeeksforGeeks Medium

N Meetings in One Room

Given meeting start and end times and one room, pick the maximum number of non-overlapping meetings.

greedysortingintervals
Open on GeeksforGeeks ↗
02

Intuition

💡

The meeting that ends earliest leaves the most room for everything after it — choosing it can never hurt. Sort by end time and greedily take every meeting that starts after the last chosen one ends.

03

Approach

1

Sort by end time

Finishing early is the only thing that matters for what fits afterward — not duration, not start time.

2

Sweep and take

Keep lastEnd. For each meeting in order, if start > lastEnd, take it and update lastEnd.

3

Why greedy is optimal

Exchange argument: any optimal schedule can swap its first meeting for the earliest-ending one without losing meetings — so the greedy prefix is always extendable to an optimum.

04

Solution & live demo

python
1def max_meetings(start, end):
2 meetings = sorted(zip(end, start)) # by end time
3 count, last_end = 0, -1
4 for e, s in meetings:
5 if s > last_end:
6 count += 1
7 last_end = e
8 return count
05

Edge cases

Meeting ends exactly when another starts

Classic GFG version requires strict start > lastEnd; back-to-back with equal times is rejected.

Ties in end time

Any order among ties works — each blocks the same suffix.

06

Complexity

Time
O(n log n)
Space
O(n)
Sort dominates; sweep is linear.