VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-trains-stoppage-can-provided/

⇱ Maximum trains for which stoppage can be provided - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum trains for which stoppage can be provided

Last Updated : 9 Jan, 2026

Given a railway station with m platforms and a 2D array trains[][] of size n × 3, where:

  • trains[i][0] denotes the arrival time of the i-th train
  • trains[i][1] denotes the departure time of the i-th train
  • trains[i][2] denotes the platform number required by the i-th train

A platform can serve only one train at a time. If a train departs at time T and another arrives at the same time T, the platform can be reused.

Each train must halt for at least one unit of time (i.e., arrival time is not equal to departure time). Trains that do not require a platform may pass through the station without stopping using the main tracks.
Find the maximum number of trains that can be scheduled to stop at the station without any platform conflicts.

Examples:

Input: m = 3

👁 table_1

Output: 5
Explanation: If train no. 1 goes through main track without stopping, then 2 and 6 can easily be accommodated on platform 1. And 3 and 4 on platform 2 and 5 on platform 3.

Input: m = 1

👁 table_2

Output: 3
Explanation: All three trains can be easily stopped at platform 1.

[Approach] Platform-wise Greedy Scheduling - O(n*log n) Time and O(1) Space

Each train is assigned to a fixed platform, so trains scheduled on different platforms never conflict with each other. This allows the problem to be broken into independent subproblems, one for each platform.

For a single platform, the task reduces to selecting the maximum number of non-overlapping train stoppages, which is equivalent to the classic interval scheduling problem. A greedy strategy is optimal in this case: whenever multiple trains overlap in time, choosing the train with the earliest departure is always beneficial, as it frees the platform sooner and allows more trains to be accommodated later.

By applying this greedy reasoning independently to each platform and summing the results, we obtain the maximum number of trains that can be provided stoppage without any platform conflicts.


Output
5
Comment
Article Tags:
Article Tags: