0
0
DSA Cprogramming~10 mins

Minimum Number of Platforms in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Minimum Number of Platforms
Sort arrival times
Sort departure times
Initialize pointers i=0, j=0
Compare arrival[i
Increment platforms
Track max platforms needed
Repeat until all trains processed
Sort arrival and departure times, then use two pointers to count platforms needed by comparing arrivals and departures.
Execution Sample
DSA C
int minPlatforms(int arr[], int dep[], int n) {
  sort(arr, arr+n);
  sort(dep, dep+n);
  int i = 0, j = 0, platforms = 0, maxPlatforms = 0;
  while (i < n && j < n) {
    if (arr[i] <= dep[j]) platforms++, i++;
    else platforms--, j++;
    maxPlatforms = max(maxPlatforms, platforms);
  }
  return maxPlatforms;
}
Calculates minimum platforms needed by sorting times and counting overlaps.
Execution Table
StepOperationi (arrival index)j (departure index)Platforms CountMax PlatformsVisual State (Arrivals vs Departures)
1Sort arrival and departure arrays0000Arrivals: [900, 940, 950, 1100, 1500, 1800] Departures: [910, 1120, 1130, 1200, 1900, 2000]
2Compare arr[0]=900 <= dep[0]=9100011Platform needed for train arriving at 900
3Increment i to 11011Platforms still 1
4Compare arr[1]=940 <= dep[0]=9101001Train at 940 arrives after 910 departure, platform freed
5Increment j to 11101Platforms now 0
6Compare arr[1]=940 <= dep[1]=11201111New train arrives before next departure, platform needed
7Increment i to 22111Platforms 1
8Compare arr[2]=950 <= dep[1]=11202122Another train arrives, platform count increases
9Increment i to 33122Platforms 2
10Compare arr[3]=1100 <= dep[1]=11203133Third train overlaps, platform count 3
11Increment i to 44133Platforms 3
12Compare arr[4]=1500 <= dep[1]=11204123Train arrives after departure, platform freed
13Increment j to 24223Platforms 2
14Compare arr[4]=1500 <= dep[2]=11304213Another departure frees platform
15Increment j to 34313Platforms 1
16Compare arr[4]=1500 <= dep[3]=12004303Departure frees platform
17Increment j to 44403Platforms 0
18Compare arr[4]=1500 <= dep[4]=19004413Train arrives before departure, platform needed
19Increment i to 55413Platforms 1
20Compare arr[5]=1800 <= dep[4]=19005423Another train arrives, platform count 2
21Increment i to 66423Platforms 2
22i reached n=6, loop ends6423All trains processed
23Return maxPlatforms---3Minimum platforms needed is 3
💡 All trains processed (i reached n=6), loop ends
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8After Step 10After Step 12After Step 18After Step 20Final
i0112344566
j0011112444
platforms0101232122
maxPlatforms0111233333
Key Moments - 3 Insights
Why do we increment 'j' (departure pointer) only when arrival[i] > departure[j]?
Because when a train arrives after another departs, a platform frees up, so we move the departure pointer to check next departure. This is shown in execution_table steps 4, 12, and 16 where platforms decrease and j increments.
Why do we track maxPlatforms separately from current platforms?
Current platforms count changes as trains arrive and depart, but maxPlatforms keeps the highest number seen so far. This ensures we know the maximum platforms needed at any time, as seen in steps 10 and 20.
What happens if arrival and departure times are equal?
If arrival[i] == departure[j], we treat it as arrival <= departure, so platforms increase. This ensures a platform is ready for the arriving train, as in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the platforms count at step 10?
A1
B2
C3
D0
💡 Hint
Check the 'Platforms Count' column at step 10 in the execution_table.
At which step does the departure pointer 'j' first increment?
AStep 5
BStep 3
CStep 7
DStep 9
💡 Hint
Look for the first step where 'j' changes from 0 to 1 in variable_tracker.
If the arrival time at index 1 was 920 instead of 940, how would platforms count change at step 4?
AIt would increase to 1
BIt would remain 0
CIt would decrease to -1
DIt would increase to 2
💡 Hint
Compare arrival[1] with departure[0] at step 4 in execution_table and consider the condition arr[i] <= dep[j].
Concept Snapshot
Minimum Number of Platforms:
- Sort arrival and departure times separately.
- Use two pointers i, j for arrivals and departures.
- If arrival[i] <= departure[j], increment platforms and i.
- Else decrement platforms and increment j.
- Track max platforms during iteration.
- Result is max platforms needed to avoid waiting.
Full Transcript
To find the minimum number of platforms needed for trains, we first sort the arrival and departure times. We use two pointers: one for arrivals and one for departures. We compare the current arrival time with the current departure time. If the arrival is earlier or equal, it means a train needs a platform, so we increase the platform count and move to the next arrival. If the arrival is later, it means a train has left, freeing a platform, so we decrease the platform count and move to the next departure. We keep track of the maximum number of platforms needed at any time. When all trains are processed, this maximum is the minimum number of platforms required to avoid waiting.