Problem Statement
Minimum Number of Platforms
You run a train station. Each train shows up at some arrival time and leaves at some departure time. While a train is sitting at the station, it needs its own platform. Your job is to find the smallest number of platforms that lets every train park without ever having to wait. The answer is simply the most trains that are ever at the station at the same moment. Think about the single busiest second of the day. If five trains overlap at that second, you need five platforms.
Signals to notice
Brute force first
For each train, count how many others overlap. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
The key insight
Sort arrivals and departures separately. Two pointers: if next event is arrival, +1 platform. If departure, -1 platform. Track the peak. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.
Trace it on arr=[900,940,950,1100,1500,1800], dep=[910,1200,1120,1130,1900,2000]
sort: arr=[900,940,950,1100,1500,1800], dep=[910,1120,1130,1200,1900,2000]; i=j=plat=res=0 arr[0]=900<=dep[0]=910 -> plat=1,res=1,i=1 arr[1]=940>dep[0]=910 -> plat=0,j=1 arr[1]=940<=dep[1]=1120 -> plat=1,i=2; arr[2]=950<=1120 -> plat=2,res=2,i=3 arr[3]=1100<=dep[1]=1120 -> plat=3,res=3,i=4 (peak) arr[4]=1500>dep[1..3]=1120,1130,1200 -> 3 departures: plat=0,j=4 arr[4]=1500<=dep[4]=1900 -> plat=1,i=5; arr[5]=1800<=1900 -> plat=2,i=6; loop ends return result = 3
What must stay true
The minimum platforms = maximum number of simultaneously present trains. The event sweep counts active trains at every transition point. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Shape of the loop
sort(arr); sort(dep) i = j = platforms = result = 0 while i < n: if arr[i] <= dep[j]: platforms++; result = max(result, platforms); i++ else: platforms--; j++ return result
Pseudocode only — the full worked solution lives in the Solution tab.
Easy way to go wrong
Processing arrivals and departures together without sorting them separately — sorting by event time (treating arrivals and departures as events on a timeline) gives the correct chronological order. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.