N Meetings in One Room
Given meeting start and end times and one room, pick the maximum number of non-overlapping meetings.
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.
Approach
Sort by end time
Finishing early is the only thing that matters for what fits afterward — not duration, not start time.
Sweep and take
Keep lastEnd. For each meeting in order, if start > lastEnd, take it and update lastEnd.
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.
Solution & live demo
Edge cases
Classic GFG version requires strict start > lastEnd; back-to-back with equal times is rejected.
Any order among ties works — each blocks the same suffix.