0% found this document useful (0 votes)
11 views6 pages

DAA Lab

The document discusses the problem of determining the minimum number of platforms required at a railway station based on the arrival and departure times of trains. It outlines a logic that involves merging and sorting the arrival and departure times, using a counter to track the number of trains present at the station. An algorithm is provided to compute the required number of platforms, with a time complexity of O(nLogn) due to sorting.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

DAA Lab

The document discusses the problem of determining the minimum number of platforms required at a railway station based on the arrival and departure times of trains. It outlines a logic that involves merging and sorting the arrival and departure times, using a counter to track the number of trains present at the station. An algorithm is provided to compute the required number of platforms, with a time complexity of O(nLogn) due to sorting.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Minimum number of

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

You might also like