We want to find the smallest number of train platforms needed so that no train waits. We do this by checking how many trains overlap in time.
Analogy: Imagine a parking lot where cars arrive and leave at different times. The minimum number of parking spots needed is the maximum number of cars parked at the same time.
Trains arrive after all others depart (no overlap)
Platforms needed is 1
DSA Typescript
if (arrivals[i] < departures[j])
When to Use This Pattern
When you see overlapping time intervals and need to find the maximum overlap, use the two-pointer approach on sorted start and end times to find the minimum resources needed.
Common Mistakes
Mistake: Not sorting arrival and departure times before processing Fix: Sort both arrays to ensure correct order of events
Mistake: Using <= instead of < in comparison, causing incorrect platform count Fix: Use strictly less than (<) to count arrivals before departures
Summary
Calculates the minimum number of platforms needed so no train waits by counting overlapping train times.
Use when scheduling resources that must handle overlapping intervals without delay.
Sort arrival and departure times separately and use two pointers to track overlaps efficiently.