Minimum number of platforms required for a railway

Greedy Algorithms Scheduling and Interval Problems Medium
  • This problem uses the algorithm concept of scheduling and resource allocation, which is broadly used in operating system design and database management system to handle multiple processes concurrently
  • A practical application can be seen in air traffic control systems, where allocating runway and airspace to numerous incoming and outgoing flights in an efficient manner becomes crucial
  • Similarly, it is used in reservation systems of hotels or even in meeting room booking systems in multi-national companies to maximize utilization and prevent overlapping bookings

Given the arrival and departure times of all trains reaching a particular railway station, determine the minimum number of platforms required so that no train is kept waiting. Consider all trains arrive and depart on the same day.


In any particular instance, the same platform cannot be used for both the departure of one train and the arrival of another train, necessitating the use of different platforms in such cases.

Examples:

Input : Arrival = [0900, 0940, 0950, 1100, 1500, 1800] , Departure = [0910, 1200, 1120, 1130, 1900, 2000]


Output : 3


Explanation : The first , second , fifth number train can use the platform 1.

The third and sixth train can use the platform 2.

The fourth train will use platform 3.

So total we need 3 different platforms for the railway station so that no train is kept waiting.

Input : Arrival = [0900, 1100, 1235] , Departure = [1000, 1200, 1240]


Output : 1


Explanation : All the three trains can use the platform 1.

So we required only 1 platform.

Input : Arrival = [0900, 1000, 1200] , Departure = [1000, 1200, 1240]

Constraints

  • 1 <= N <= 105
  • 0000 <= Arrival[i] <= Departure[i] <= 2359

Hints

  • Create two separate arrays: one for arrival times and one for departure times. Sort both arrays independently.
  • Use two pointers. One pointer for arrival times and another for departure times. Traverse the arrays. If a train arrives before the previous train departs, increment the platform count.

Company Tags

Oracle Medtronic GE Healthcare HCL Technologies Zynga Reddit Electronic Arts Rakuten Byju's Airbnb Broadcom Ernst & Young Square Qualcomm Flipkart Rockstar Games Twilio Seagate Technology Micron Technology Stripe Alibaba Johnson & Johnson Dropbox American Express Salesforce Google Microsoft Amazon Meta Apple Netflix Adobe