DAA Lab
DAA Lab
Platforms required on a
Railway Station
UVIKA RAINA(16CP206)
MEET MEHTA(16CP203)
Problem?
Given arrival and departure times of
all trains that reach a railway
station, find the minimum number of
platforms required for the railway
station so that no train waits.
Given two arrays which represent
arrival and departure times of trains
that stop.
Logic?
• The idea is to merge arrival and departure time of trains and
consider them in sorted order.
• We maintain a counter to count number of trains present at the
station at any point of time. The counter also represents number
of platforms needed at that time.
• If train is scheduled to arrive next, we increase the counter by 1
and update minimum platforms needed if count is more than
minimum platforms needed so far.
• If train is scheduled to depart next, we decrease the counter by 1.
• One special case we need to handle. When two trains are
scheduled to arrive and depart at the same time, we depart the
train first.
Algorithm?
• int findPlatform(int arr[], int dep[], int n){
• int plat_needed = 1, result = 1;
• int i = 1, j = 0;
• while (i < n && j < n){
• if (arr[i] < dep[j]){
• plat_needed++;
• i++;
• if (plat_needed > result) // Update result if needed
• result = plat_needed;}
• else {
• plat_needed--;
• j++; }}
• return result;}
Time Complexity?
• O(nLogn), assuming that a O(nLogn) sorting
algorithm for sorting arr[] and dep[].
THANK YOU