Minimum number of railway platforms
Coding problemMediumAsked in: Adobe, Amazon, Atlassian, Google, Microsoft, Walmart
A station receives n trains in a day. Train i arrives at minute arrivals[i] and departs at minute departures[i] (minutes after midnight). A platform is the place where a train stops. A train uses a platform from its arrival to its departure, including both minutes.
Return the smallest number of platforms needed so that no train has to wait. If one train departs in the same minute another arrives, they need two platforms. With no trains, the answer is 0.
Examples
Example 1
Input: arrivals = [540,580,590,660,900,1080], departures = [550,720,680,690,1140,1200]
Output: 3
At minute 660 three trains are at the station.
Example 2
Input: arrivals = [600,600], departures = [600,610]
Output: 2
Example 3
Input: arrivals = [600,610], departures = [605,620]
Output: 1
Constraints
- Both lists hold n values, n between 0 and 100,000
- Each time is between 0 and 1,439, and departures[i] is at least arrivals[i]
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.