oneby7

Intervals Merging and scheduling

513. Minimum Platforms

Medium

A railway station has to serve n trains. Train i arrives at time arr[i] and departs at time dep[i]. Times are whole numbers. While a train is at the station it needs a platform to itself, and it keeps the same platform for its whole stay. Return the smallest number of platforms the station needs so that no train ever waits for one.

A train holds its platform from its arrival to its departure, including both moments. So when one train departs at time t and another arrives at time t, both are at the station at t, and they need two platforms. A train with arr[i] = dep[i] still needs a platform at that moment.

Meeting Rooms II uses the other rule: there, a meeting that ends at time t frees its room in time for a meeting that starts at t.

Example 1

Input: arr = [3, 1, 10, 4, 7], dep = [6, 4, 12, 9, 8]

Output: 3

Explanation: At time 4, train 1 departs and train 3 arrives, while train 0 is still at the station. All three need a platform at that moment. At every other moment, at most two trains are at the station.

Example 2

Input: arr = [0, 0, 0], dep = [0, 1000000000, 0]

Output: 3

Explanation: All three trains are at the station at time 0. Trains 0 and 2 depart the moment they arrive, but each still needs a platform for that moment.

Example 3

Input: arr = [5, 1, 8], dep = [6, 10, 9]

Output: 2

Explanation: Train 1 stays from time 1 to time 10. Trains 0 and 2 are never at the station together, so they can use the same second platform, one after the other.

Check your understanding

Work out the answer for this input before you write any code.

arr = [4, 1, 8, 2], dep = [7, 10, 9, 4]

Constraints

  • 1 ≤ arr.length ≤ 105
  • dep.length = arr.length
  • 0 ≤ arr[i] ≤ dep[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.