 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Number of Platforms Problem
A list of arrival and departure time is given. Now the problem is to find the minimum number of platforms are required for the railway as no train waits.
By sorting all timings in sorted order, we can find the solution easily, it will be easy to track when the train has arrived but not left the station.
The time complexity of this problem is O(n Log n).
Input and Output
Input: Lists of arrival time and departure time. Arrival: {900, 940, 950, 1100, 1500, 1800} Departure: {910, 1200, 1120, 1130, 1900, 2000} Output: Minimum Number of Platforms Required: 3  Algorithm
minPlatform(arrival, departure, int n)
Input − The list of arrival time and the departure times, and the number of items in the list
Output − The number of minimum platforms is needed to solve the problem.
Begin sort arrival time list, and departure time list platform := 1 and minPlatform := 1 i := 1 and j := 0 for elements in arrival list ‘i’ and departure list ‘j’ do if arrival[i] < departure[j] then platform := platform + 1 i := i+1 if platform > minPlatform then minPlatform := platform else platform := platform – 1 j := j + 1 done return minPlatform End
Example
#include<iostream> #include<algorithm> using namespace std; int minPlatform(int arrival[], int departure[], int n) {    sort(arrival, arrival+n);     //sort arrival and departure times    sort(departure, departure+n);    int platform = 1, minPlatform = 1;    int i = 1, j = 0;    while (i < n && j < n) {       if (arrival[i] < departure[j]) {          platform++;     //platform added          i++;          if (platform > minPlatform)    //if platform value is greater, update minPlatform             minPlatform = platform;       } else {          platform--;      //delete platform          j++;       }    }    return minPlatform; } int main() {    int arrival[] = {900, 940, 950, 1100, 1500, 1800};    int departure[] = {910, 1200, 1120, 1130, 1900, 2000};    int n = 6;    cout << "Minimum Number of Platforms Required: " << minPlatform(arrival, departure, n); }  Output
Minimum Number of Platforms Required: 3
Advertisements
 