0
0
DSA Typescriptprogramming~10 mins

Minimum Number of Platforms in DSA Typescript - 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
Move i or j pointer
Update max platforms
Repeat until all trains processed
Sort arrival and departure times, then use two pointers to track platforms needed by comparing arrivals and departures.
Execution Sample
DSA Typescript
const arrival = [900, 940, 950, 1100, 1500, 1800];
const departure = [910, 1200, 1120, 1130, 1900, 2000];
// Find minimum platforms needed
This code finds the minimum number of platforms needed so that no train waits.
Execution Table
StepOperationi (arrival index)j (departure index)Current PlatformsMax PlatformsVisual State
1Compare arrival[0]=900 and departure[0]=9100011Platforms: 1 (Train at 900 arrived)
2arrival[1]=940 < departure[0]=910? No, departure[0] < arrival[1]1001Platforms: 0 (Train at 900 departed)
3Move j to 11101Platforms: 0
4Compare arrival[1]=940 and departure[1]=12001111Platforms: 1 (Train at 940 arrived)
5Compare arrival[2]=950 and departure[1]=12002122Platforms: 2 (Train at 950 arrived)
6Compare arrival[3]=1100 and departure[1]=12003133Platforms: 3 (Train at 1100 arrived)
7Compare arrival[4]=1500 and departure[1]=12004123Platforms: 2 (Train at 1200 departed)
8Move j to 24223Platforms: 2
9Compare arrival[4]=1500 and departure[2]=11204213Platforms: 1 (Train at 1120 departed)
10Move j to 34313Platforms: 1
11Compare arrival[4]=1500 and departure[3]=11304303Platforms: 0 (Train at 1130 departed)
12Move j to 44403Platforms: 0
13Compare arrival[4]=1500 and departure[4]=19004413Platforms: 1 (Train at 1500 arrived)
14Compare arrival[5]=1800 and departure[4]=19005423Platforms: 2 (Train at 1800 arrived)
15Compare arrival[6]=undefined and departure[4]=19006423No more arrivals, process departures
16Move j to 56513Platforms: 1 (Train at 1900 departed)
17Move j to 66603Platforms: 0 (Train at 2000 departed)
18End6603All trains processed
💡 All trains processed: i and j pointers reached end of arrival and departure arrays.
Variable Tracker
VariableStartAfter Step 1After Step 5After Step 10After Step 15Final
i (arrival index)013466
j (departure index)001346
Current Platforms013120
Max Platforms013333
Key Moments - 3 Insights
Why do we increment 'j' (departure pointer) when departure[j] < arrival[i]?
Because a train has left before the next train arrives, so one platform is freed. See execution_table steps 2, 7, 9.
Why do we update max platforms only when arrival[i] < departure[j]?
Because a new train arrives before any train leaves, increasing platform need. See execution_table steps 1, 5, 6.
What happens when all arrivals are processed but departures remain?
We keep moving the departure pointer to free platforms until all trains have left. See execution_table steps 15-17.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, how many platforms are currently needed?
A1
B2
C3
D0
💡 Hint
Check the 'Current Platforms' column at step 6 in execution_table.
At which step does the number of platforms first decrease from 3 to 2?
AStep 7
BStep 5
CStep 10
DStep 2
💡 Hint
Look for when 'Current Platforms' changes from 3 to 2 in execution_table.
If arrival[2] was 1300 instead of 950, how would max platforms change?
AMax platforms would increase
BMax platforms would decrease
CMax platforms would stay the same
DCannot determine
💡 Hint
Consider how later arrival affects overlapping trains and platform count in variable_tracker.
Concept Snapshot
Minimum Number of Platforms:
- Sort arrival and departure times separately.
- Use two pointers i, j for arrivals and departures.
- Increment platforms if arrival[i] < departure[j].
- Decrement platforms if departure[j] <= arrival[i].
- Track max platforms needed during traversal.
Full Transcript
To find the minimum number of platforms needed for trains, first sort the arrival and departure times. Then use two pointers to compare the next arrival and departure. If a train arrives before the earliest departure, increase platform count. If a train departs before the next arrival, decrease platform count. Keep track of the maximum platforms needed at any time. Continue until all trains are processed. This ensures no train waits for a platform.