Discover how a simple sorting trick can solve the chaos of train platform assignments!
Why Minimum Number of Platforms in DSA C?
Imagine a busy train station where many trains arrive and depart at different times. You need to assign platforms so that no two trains are on the same platform at the same time.
Without a clear method, you try to check each train's arrival and departure manually to find how many platforms are needed.
Manually checking each train's schedule is slow and confusing. You might miss overlapping times or count platforms incorrectly.
This leads to mistakes like assigning too few platforms, causing trains to wait, or too many, wasting space.
The Minimum Number of Platforms concept helps by sorting arrival and departure times separately and then counting overlaps efficiently.
This way, you quickly find the exact number of platforms needed without confusion or errors.
int countPlatforms(int arrivals[], int departures[], int n) {
int platforms = 0;
for (int i = 0; i < n; i++) {
int needed = 1;
for (int j = 0; j < n; j++) {
if (i != j && arrivals[j] < departures[i] && departures[j] > arrivals[i]) {
needed++;
}
}
if (needed > platforms) platforms = needed;
}
return platforms;
}int findMinPlatforms(int arrivals[], int departures[], int n) {
sort(arrivals, arrivals + n);
sort(departures, departures + n);
int platform_needed = 1, max_platforms = 1;
int i = 1, j = 0;
while (i < n && j < n) {
if (arrivals[i] <= departures[j]) {
platform_needed++;
i++;
if (platform_needed > max_platforms) max_platforms = platform_needed;
} else {
platform_needed--;
j++;
}
}
return max_platforms;
}This concept enables efficient scheduling and resource allocation in busy systems like train stations, airports, or server task management.
Railway stations use this method to decide how many platforms are needed so trains can arrive and depart smoothly without delays.
Manual checking of overlapping train times is slow and error-prone.
Sorting arrivals and departures separately helps count overlaps efficiently.
Minimum Number of Platforms gives the exact number of platforms needed to avoid conflicts.