Pattern visualizer
Minimum Platforms Required
Pairing each train with its own arrival and departure hides the answer, because what matters is not any single train but how many are standing at the same instant. Split every train into two independent timeline events — an arrival that takes a platform, a departure that frees one — sort them by clock time, and walk them in order. A running counter then IS the number of trains at the station at that moment, and the largest value it ever reaches is the number of platforms needed. Ties matter: an arrival at the same minute as a departure is processed first, because both trains are briefly present. Animated on: arrivals = [900, 940, 950, 1100, 1500], departures = [910, 1200, 1120, 1130, 1900] — what is the fewest platforms the station can run with so no train ever waits?.
Chronological sweep of arrivals and departures
5 trains become 10 timeline events, listed train by train: arrival then departure for train 1, then train 2, and so on. In this order the row jumps back and forth in time (900 then 910 then 940), so counting how many trains are standing at once is impossible yet.
1FUNCTION minPlatforms(arr, dep)2 events <- EMPTY LIST3 FOR i <- 0 TO LENGTH(arr) - 14 APPEND (arr[i], +1) TO events5 APPEND (dep[i], -1) TO events6 SORT events BY TIME ASCENDING, ARRIVALS BEFORE DEPARTURES7 platforms <- 08 best <- 09 FOR EACH (time, delta) IN events10 platforms <- platforms + delta11 best <- MAX(best, platforms)12 RETURN best
← / → step · space play · Home restart