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, max_platforms=0
i=0, j=0, platforms=0, max_platforms=0
Why: We start checking from the earliest arrival and departure.
Step 3: Compare arrival[i]=900 and departure[j]=910; since 900 < 910, increment platforms and i
platforms=1, max_platforms=1, i=1, j=0
Why: A train arrives before the first departure, so we need a platform.
Step 4: Compare arrival[i]=940 and departure[j]=910; since 940 > 910, decrement platforms and increment j
platforms=0, max_platforms=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; since 940 < 1120, increment platforms and i
platforms=1, max_platforms=1, i=2, j=1
Why: A train arrives before next departure, need a platform.
Step 6: Compare arrival[i]=950 and departure[j]=1120; since 950 < 1120, increment platforms and i
platforms=2, max_platforms=2, i=3, j=1
Why: Another train arrives before departure, need another platform.
Step 7: Compare arrival[i]=1100 and departure[j]=1120; since 1100 < 1120, increment platforms and i
platforms=3, max_platforms=3, i=4, j=1
Why: Another train arrives before departure, need another platform.
Step 8: Compare arrival[i]=1500 and departure[j]=1120; since 1500 > 1120, decrement platforms and j
platforms=2, max_platforms=3, i=4, j=2
Why: A train departs, freeing a platform.
Step 9: Compare arrival[i]=1500 and departure[j]=1130; since 1500 > 1130, decrement platforms and j
platforms=1, max_platforms=3, i=4, j=3
Why: Another train departs, freeing a platform.
Step 10: Compare arrival[i]=1500 and departure[j]=1200; since 1500 > 1200, decrement platforms and j
platforms=0, max_platforms=3, i=4, j=4
Why: Another train departs, freeing a platform.
Step 11: Compare arrival[i]=1500 and departure[j]=1900; since 1500 < 1900, increment platforms and i
platforms=1, max_platforms=3, i=5, j=4
Why: A train arrives before departure, need a platform.
Step 12: Compare arrival[i]=1800 and departure[j]=1900; since 1800 < 1900, increment platforms and i
platforms=2, max_platforms=3, i=6, j=4
Why: Another train arrives before departure, need another platform.
Step 13: No more arrivals; decrement platforms as departures happen
platforms=1, max_platforms=3, i=6, j=5
Why: Trains depart, freeing platforms.
Step 14: Final departure frees last platform
platforms=0, max_platforms=3, i=6, j=6
Why: All trains have departed.
Result: Final max_platforms = 3
Platforms needed: 3