Discover how to solve a real-world scheduling puzzle with simple sorting and counting!
Why Minimum Number of Platforms in DSA Typescript?
Imagine a busy train station where many trains arrive and depart at different times. You want to know how many platforms are needed so that no train has to wait for a platform to be free.
Trying to count platforms manually by checking each train's arrival and departure times is slow and confusing. It's easy to make mistakes, especially when many trains overlap in time.
The "Minimum Number of Platforms" method helps us quickly find the smallest number of platforms needed by sorting arrival and departure times and counting overlaps efficiently.
let platforms = 0; for (let i = 0; i < trains.length; i++) { for (let j = 0; j < trains.length; j++) { if (trains[i].arrival < trains[j].departure && trains[j].arrival < trains[i].departure) { platforms++; } } }
let arrivals = trains.map(t => t.arrival).sort((a, b) => a - b); let departures = trains.map(t => t.departure).sort((a, b) => a - b); let platformNeeded = 0, maxPlatforms = 0; let i = 0, j = 0; while (i < arrivals.length && j < departures.length) { if (arrivals[i] < departures[j]) { platformNeeded++; maxPlatforms = Math.max(maxPlatforms, platformNeeded); i++; } else { platformNeeded--; j++; } }
This concept lets you efficiently plan resources to avoid delays and overcrowding in busy schedules.
Railway stations use this to decide how many platforms to build so trains can arrive and leave smoothly without waiting.
Manual checking of overlapping trains is slow and error-prone.
Sorting arrivals and departures helps count overlaps quickly.
Minimum platforms needed equals the maximum number of overlapping trains.