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
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
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.
CPP
#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;intmaxStop(intplat,vector<vector<int>>&trains){// number of trainsintn=trains.size();// vector for each platformvector<vector<pair<int,int>>>arr(plat+1);// Store (departure, arrival) for each platformfor(inti=0;i<n;i++){intarrival=trains[i][0];intdeparture=trains[i][1];intplatform=trains[i][2];arr[platform].push_back({departure,arrival});}intcount=0;// Process each platform independentlyfor(inti=1;i<=plat;i++){if(arr[i].empty())continue;// Sort by departure timesort(arr[i].begin(),arr[i].end());// Select first trainintlastDeparture=arr[i][0].first;count++;// Select remaining compatible trainsfor(intj=1;j<arr[i].size();j++){if(arr[i][j].second>=lastDeparture){count++;lastDeparture=arr[i][j].first;}}}returncount;}intmain(){// number of platformsintplat=3;vector<vector<int>>trains={{1000,1030,1},{1010,1030,1},{1000,1020,2},{1030,1230,2},{1200,1230,3},{900,1005,1}};cout<<maxStop(plat,trains);return0;}
Java
importjava.util.*;classGFG{staticintmaxStop(intplat,int[][]trains){// number of trainsintn=trains.length;// vector for each platformArrayList<ArrayList<int[]>>arr=newArrayList<>();for(inti=0;i<=plat;i++){arr.add(newArrayList<>());}// Store (departure, arrival) for each platformfor(inti=0;i<n;i++){intarrival=trains[i][0];intdeparture=trains[i][1];intplatform=trains[i][2];arr.get(platform).add(newint[]{departure,arrival});}intcount=0;// Process each platform independentlyfor(inti=1;i<=plat;i++){if(arr.get(i).isEmpty())continue;// Sort by departure timearr.get(i).sort((a,b)->a[0]-b[0]);// Select first trainintlastDeparture=arr.get(i).get(0)[0];count++;// Select remaining compatible trainsfor(intj=1;j<arr.get(i).size();j++){if(arr.get(i).get(j)[1]>=lastDeparture){count++;lastDeparture=arr.get(i).get(j)[0];}}}returncount;}publicstaticvoidmain(String[]args){// number of platformsintplat=3;int[][]trains={{1000,1030,1},{1010,1030,1},{1000,1020,2},{1030,1230,2},{1200,1230,3},{900,1005,1}};System.out.println(maxStop(plat,trains));}}
Python
defmaxStop(plat,trains):# number of trainsn=len(trains)# vector for each platformarr=[[]for_inrange(plat+1)]# Store (departure, arrival) for each platformforiinrange(n):arrival=trains[i][0]departure=trains[i][1]platform=trains[i][2]arr[platform].append((departure,arrival))count=0# Process each platform independentlyforiinrange(1,plat+1):ifnotarr[i]:continue# Sort by departure timearr[i].sort()# Select first trainlastDeparture=arr[i][0][0]count+=1# Select remaining compatible trainsforjinrange(1,len(arr[i])):ifarr[i][j][1]>=lastDeparture:count+=1lastDeparture=arr[i][j][0]returncountif__name__=="__main__":# number of platformsplat=3trains=[[1000,1030,1],[1010,1030,1],[1000,1020,2],[1030,1230,2],[1200,1230,3],[900,1005,1]]print(maxStop(plat,trains))
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticintmaxStop(intplat,int[,]trains){// number of trainsintn=trains.GetLength(0);// vector for each platformList<List<int[]>>arr=newList<List<int[]>>();for(inti=0;i<=plat;i++){arr.Add(newList<int[]>());}// Store (departure, arrival) for each platformfor(inti=0;i<n;i++){intarrival=trains[i,0];intdeparture=trains[i,1];intplatform=trains[i,2];arr[platform].Add(newint[]{departure,arrival});}intcount=0;// Process each platform independentlyfor(inti=1;i<=plat;i++){if(arr[i].Count==0)continue;// Sort by departure timearr[i].Sort((a,b)=>a[0].CompareTo(b[0]));// Select first trainintlastDeparture=arr[i][0][0];count++;// Select remaining compatible trainsfor(intj=1;j<arr[i].Count;j++){if(arr[i][j][1]>=lastDeparture){count++;lastDeparture=arr[i][j][0];}}}returncount;}staticvoidMain(){// number of platformsintplat=3;int[,]trains={{1000,1030,1},{1010,1030,1},{1000,1020,2},{1030,1230,2},{1200,1230,3},{900,1005,1}};Console.WriteLine(maxStop(plat,trains));}}
JavaScript
functionmaxStop(plat,trains){// number of trainsletn=trains.length;// vector for each platformletarr=Array.from({length:plat+1},()=>[]);// Store (departure, arrival) for each platformfor(leti=0;i<n;i++){letarrival=trains[i][0];letdeparture=trains[i][1];letplatform=trains[i][2];arr[platform].push([departure,arrival]);}letcount=0;// Process each platform independentlyfor(leti=1;i<=plat;i++){if(arr[i].length===0)continue;// Sort by departure timearr[i].sort((a,b)=>a[0]-b[0]);// Select first trainletlastDeparture=arr[i][0][0];count++;// Select remaining compatible trainsfor(letj=1;j<arr[i].length;j++){if(arr[i][j][1]>=lastDeparture){count++;lastDeparture=arr[i][j][0];}}}returncount;}// Driver function// number of platformsletplat=3;lettrains=[[1000,1030,1],[1010,1030,1],[1000,1020,2],[1030,1230,2],[1200,1230,3],[900,1005,1]];console.log(maxStop(plat,trains));