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.
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of platforms required so that no train has to wait for another to leave.Step 2: Clarify what is being counted
We count how many trains overlap in time, which determines how many platforms are needed simultaneously.Final Answer:
The minimum number of platforms needed so no train waits -> Option BQuick Check:
Minimum platforms = max trains overlapping [OK]
- Confusing total trains with platforms needed
- Ignoring overlapping times
- Counting only arrivals or departures
Solution
Step 1: Check array declaration syntax in C
Arrays must be declared with size or empty brackets and initialized with curly braces {}.Step 2: Validate each option
int arrival[5] = {900, 940, 950, 1100, 1500}; int departure[5] = {910, 1200, 1120, 1130, 1900}; uses correct syntax with size and braces. Others use incorrect syntax like parentheses or missing braces.Final Answer:
int arrival[5] = {900, 940, 950, 1100, 1500}; int departure[5] = {910, 1200, 1120, 1130, 1900}; -> Option AQuick Check:
Arrays use {} with size or empty brackets [OK]
- Using parentheses () instead of braces {}
- Missing array size or empty brackets
- Assigning values without braces
int arrival[] = {900, 940, 950, 1100, 1500};
int departure[] = {910, 1200, 1120, 1130, 1900};
int n = 5;
// Assume arrays are sorted
int i = 1, j = 0, platforms = 1, result = 1;
while (i < n && j < n) {
if (arrival[i] <= departure[j]) {
platforms++;
i++;
if (platforms > result) result = platforms;
} else {
platforms--;
j++;
}
}
printf("%d", result);Solution
Step 1: Trace the platform count with given times
Starts platforms=1 (900 arrival), i=1, j=0. 940 <= 910? No, so platforms--, j=1. Then 940 <= 1200: platforms++, i=2. 950 <= 1200: platforms++, i=3. 1100 <= 1200: platforms++, i=4 (max=3). Later departures reduce platforms.Step 2: Find max platforms during iteration
Maximum platforms reached is 3 (trains 940-1200, 950-1120, 1100-1130 overlap).Final Answer:
3 -> Option DQuick Check:
Max overlapping trains = 3 [OK]
- Not sorting arrival and departure arrays
- Incorrectly incrementing or decrementing platform count
- Confusing <= with < in comparisons
int arrival[] = {900, 940, 950, 1100, 1500};
int departure[] = {910, 1200, 1120, 1130, 1900};
int n = 5;
int i = 0, j = 0, platforms = 0, result = 0;
while (i < n && j < n) {
if (arrival[i] < departure[j]) {
platforms++;
i++;
if (platforms > result) result = platforms;
} else {
platforms--;
j++;
}
}
printf("%d", result);Solution
Step 1: Verify initialization and first steps
platforms=0, i=0, j=0 correct. 900 < 910: platforms=1, i=1, result=1.Step 2: Check logic and trace
940 < 910? No: platforms=0, j=1. Then 940 < 1200: platforms=1, i=2; 950<1200:2, i=3; 1100<1200:3, i=4 (max=3). Continues correctly. < handles equal times optimally.Final Answer:
No error, code is correct -> Option AQuick Check:
Code outputs 3, matching overlaps [OK]
- Using arrival[i] <= departure[j] (overcounts on equal times)
- Initializing platforms=1 with i=0 (overcounts first arrival)
- Loop i <= n && j <= n (off-by-one)
Solution
Step 1: Understand impact of equal arrival and departure times
If a train departs at the same time another arrives, the platform can be reused without adding a new one.Step 2: Adjust comparison logic
When arrival[i] == departure[j], count departure first to free platform before arrival increments platform count.Final Answer:
Treat arrival[i] == departure[j] as departure first to free platform -> Option CQuick Check:
Departure before arrival frees platform [OK]
- Counting arrival before departure causing extra platform
- Ignoring equal times leading to wrong count
- Adding platforms unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
