0
0
DSA Typescriptprogramming~3 mins

Why Minimum Number of Platforms in DSA Typescript?

Choose your learning style9 modes available
The Big Idea

Discover how to solve a real-world scheduling puzzle with simple sorting and counting!

The Scenario

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.

The Problem

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 Solution

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.

Before vs After
Before
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++;
    }
  }
}
After
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++;
  }
}
What It Enables

This concept lets you efficiently plan resources to avoid delays and overcrowding in busy schedules.

Real Life Example

Railway stations use this to decide how many platforms to build so trains can arrive and leave smoothly without waiting.

Key Takeaways

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.