Step 1: Sort arrivals and departures separately
arrivals: [900, 940, 950, 1100, 1500, 1800]
departures: [910, 1120, 1130, 1200, 1900, 2000]
Why: Sorting helps us process events in chronological order.
Step 2: Initialize pointers i=0 (arrivals), j=0 (departures), platforms=0, maxPlatforms=0
i=0, j=0, platforms=0, maxPlatforms=0
Why: We track arrivals and departures to count overlapping trains.
Step 3: Compare arrival[i]=900 and departure[j]=910; arrival <= departure, so platforms++ and i++
platforms=1, maxPlatforms=1, i=1, j=0
Why: A train arrives before the first train departs, so we need a platform.
Step 4: Compare arrival[i]=940 and departure[j]=910; arrival > departure, so platforms-- and j++
platforms=0, maxPlatforms=1, i=1, j=1
Why: A train departs before next arrival, freeing a platform.
Step 5: Compare arrival[i]=940 and departure[j]=1120; arrival <= departure, so platforms++ and i++
platforms=1, maxPlatforms=1, i=2, j=1
Why: Train arrives before next departure, need platform.
Step 6: Compare arrival[i]=950 and departure[j]=1120; arrival <= departure, so platforms++ and i++
platforms=2, maxPlatforms=2, i=3, j=1
Why: Another train arrives before departure, need another platform.
Step 7: Compare arrival[i]=1100 and departure[j]=1120; arrival <= departure, so platforms++ and i++
platforms=3, maxPlatforms=3, i=4, j=1
Why: Third train arrives before departure, need third platform.
Step 8: Compare arrival[i]=1500 and departure[j]=1120; arrival > departure, so platforms-- and j++
platforms=2, maxPlatforms=3, i=4, j=2
Why: Train departs, freeing a platform.
Step 9: Compare arrival[i]=1500 and departure[j]=1130; arrival > departure, so platforms-- and j++
platforms=1, maxPlatforms=3, i=4, j=3
Why: Another train departs, freeing a platform.
Step 10: Compare arrival[i]=1500 and departure[j]=1200; arrival > departure, so platforms-- and j++
platforms=0, maxPlatforms=3, i=4, j=4
Why: Another train departs, freeing a platform.
Step 11: Compare arrival[i]=1500 and departure[j]=1900; arrival <= departure, so platforms++ and i++
platforms=1, maxPlatforms=3, i=5, j=4
Why: Train arrives before next departure, need platform.
Step 12: Compare arrival[i]=1800 and departure[j]=1900; arrival <= departure, so platforms++ and i++
platforms=2, maxPlatforms=3, i=6, j=4
Why: Another train arrives before departure, need platform.
Step 13: No more arrivals; decrement platforms as trains depart
platforms=1, maxPlatforms=3, j=5
platforms=0, maxPlatforms=3, j=6
Why: Trains depart, freeing platforms.
Result: Final maxPlatforms = 3
Minimum platforms needed: 3