FunctionRacerProblemsRace

Fewest Platforms

MediumGreedyPython, JavaScript, TypeScript, Java, C++

Each item of trains is [arrival, departure] with arrival <= departure: that train occupies a platform from minute arrival to minute departure, both included. Two trains can use the same platform only if one of them departs strictly before the other arrives; a train that arrives at the very minute another departs needs a different platform. The trains are in no particular order.

Return the fewest platforms that let every train be handled this way. The fewest number is unique, so no tie rule is needed. Return 0 when trains is empty.

Example 1

trains [[1,3],[2,4],[3,5],[6,7]]
returns 3

At minute 3 the first three trains are all present (the first one departs at 3, which still counts), so three platforms are needed and three suffice.

Example 2

trains [[1,3],[3,5]]
returns 2

The first train departs at 3 and the second arrives at 3, so they cannot share.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems