Step 1: Sort arrivals and departures separately
arrivals: [9:00, 9:40, 9:50, 11:00, 15:00, 18:00]
departures: [9:10, 11:20, 11:30, 12:00, 19:00, 20:00]
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 track how many trains are currently at the station.
Step 3: Compare arrival[0]=9:00 with departure[0]=9:10; arrival earlier, so platforms++
platforms=1, max_platforms=1, i=1, j=0
Why: A train arrives before any departs, so we need a platform.
Step 4: Compare arrival[1]=9:40 with departure[0]=9:10; departure earlier, so platforms--
platforms=0, max_platforms=1, i=1, j=1
Why: A train departs freeing a platform before next arrival.
Step 5: Compare arrival[1]=9:40 with departure[1]=11:20; arrival earlier, so platforms++
platforms=1, max_platforms=1, i=2, j=1
Why: Train arrives before next departure, need platform.
Step 6: Compare arrival[2]=9:50 with departure[1]=11:20; arrival earlier, so platforms++
platforms=2, max_platforms=2, i=3, j=1
Why: Another train arrives before departure, need another platform.
Step 7: Compare arrival[3]=11:00 with departure[1]=11:20; arrival earlier, so platforms++
platforms=3, max_platforms=3, i=4, j=1
Why: Third train arrives before departure, need third platform.
Step 8: Compare arrival[4]=15:00 with departure[1]=11:20; departure earlier, so platforms--
platforms=2, max_platforms=3, i=4, j=2
Why: Train departs freeing a platform.
Step 9: Compare arrival[4]=15:00 with departure[2]=11:30; departure earlier, so platforms--
platforms=1, max_platforms=3, i=4, j=3
Why: Another train departs freeing a platform.
Step 10: Compare arrival[4]=15:00 with departure[3]=12:00; departure earlier, so platforms--
platforms=0, max_platforms=3, i=4, j=4
Why: Another train departs freeing a platform.
Step 11: Compare arrival[4]=15:00 with departure[4]=19:00; arrival earlier, so platforms++
platforms=1, max_platforms=3, i=5, j=4
Why: Train arrives before next departure, need platform.
Step 12: Compare arrival[5]=18:00 with departure[4]=19:00; arrival earlier, so platforms++
platforms=2, max_platforms=3, i=6, j=4
Why: Another train arrives before departure, need platform.
Step 13: No more arrivals; process remaining departures, platforms-- until zero
platforms=1 after departure[4], platforms=0 after departure[5]
Why: Trains depart freeing platforms.
Result: Final max_platforms = 3
Minimum platforms needed: 3