Given the arrival and departure times of all trains reaching a particular railway station, determine the minimum number of platforms required so that no train is kept waiting. Consider all trains arrive and depart on the same day.
In any particular instance, the same platform cannot be used for both the departure of one train and the arrival of another train, necessitating the use of different platforms in such cases.
Input : Arrival = [0900, 0940, 0950, 1100, 1500, 1800] , Departure = [0910, 1200, 1120, 1130, 1900, 2000]
Output : 3
Explanation : The first , second , fifth number train can use the platform 1.
The third and sixth train can use the platform 2.
The fourth train will use platform 3.
So total we need 3 different platforms for the railway station so that no train is kept waiting.
Input : Arrival = [0900, 1100, 1235] , Departure = [1000, 1200, 1240]
Output : 1
Explanation : All the three trains can use the platform 1.
So we required only 1 platform.
Input : Arrival = [0900, 1000, 1200] , Departure = [1000, 1200, 1240]
Counting of Overlapping Trains
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to find minimum number of platforms required
int findPlatform(vector<int>& Arrival, vector<int>& Departure) {
int n = Arrival.size();
// To store the result
int ans = 1;
// Iterate on the trains platforms
for (int i = 0; i < n; i++) {
int count = 1;
// Iterate on the all the trains
for (int j = 0; j < n; j++) {
// Check with other trains
if(i != j) {
// Check for the overlapping trains
if ((Arrival[i] >= Arrival[j] && Departure[j] >= Arrival[i])) {
// Increment count
count++;
}
// Update the minimum platforms needed
ans = max(ans, count);
}
}
}
// Return number of platforms
return ans;
}
};
int main() {
vector<int> arr = {900, 945, 955, 1100, 1500, 1800};
vector<int> dep = {920, 1200, 1130, 1150, 1900, 2000};
int n = dep.size();
// Creating an instance of Solution class
Solution sol;
// Function call to find minimum number of platforms required
int ans = sol.findPlatform(arr, dep);
cout << "Minimum number of Platforms required: " << ans << endl;
}
class Solution {
// Function to find minimum number of platforms required
public int findPlatform(int[] Arrival, int[] Departure) {
int n = Arrival.length;
// To store the result
int ans = 1;
// Iterate on the trains platforms
for (int i = 0; i < n; i++) {
int count = 1;
// Iterate on all the trains
for (int j = 0; j < n; j++) {
// Check with other trains
if (i != j) {
// Check for the overlapping trains
if ((Arrival[i] >= Arrival[j] && Departure[j] >= Arrival[i])) {
// Increment count
count++;
}
// Update the minimum platforms needed
ans = Math.max(ans, count);
}
}
}
// Return number of platforms
return ans;
}
}
class Main {
public static void main(String[] args) {
int[] arr = {900, 945, 955, 1100, 1500, 1800};
int[] dep = {920, 1200, 1130, 1150, 1900, 2000};
// Creating an instance of Solution class
Solution sol = new Solution();
// Function call to find minimum number of platforms required
int ans = sol.findPlatform(arr, dep);
System.out.println("Minimum number of Platforms required: " + ans);
}
}
class Solution:
# Function to find minimum number of platforms required
def findPlatform(self, Arrival, Departure):
n = len(Arrival)
# To store the result
ans = 1
# Iterate on the trains platforms
for i in range(n):
count = 1
# Iterate on all the trains
for j in range(n):
# Check with other trains
if i != j:
# Check for the overlapping trains
if Arrival[i] >= Arrival[j] and Departure[j] >= Arrival[i]:
# Increment count
count += 1
# Update the minimum platforms needed
ans = max(ans, count)
# Return number of platforms
return ans
if __name__ == "__main__":
arr = [900, 945, 955, 1100, 1500, 1800]
dep = [920, 1200, 1130, 1150, 1900, 2000]
# Creating an instance of Solution class
sol = Solution()
# Function call to find minimum number of platforms required
ans = sol.findPlatform(arr, dep)
print("Minimum number of Platforms required:", ans)
class Solution {
// Function to find minimum number of platforms required
findPlatform(Arrival, Departure) {
let n = Arrival.length;
// To store the result
let ans = 1;
// Iterate on the trains platforms
for (let i = 0; i < n; i++) {
let count = 1;
// Iterate on all the trains
for (let j = 0; j < n; j++) {
// Check with other trains
if (i !== j) {
// Check for the overlapping trains
if (Arrival[i] >= Arrival[j] && Departure[j] >= Arrival[i]) {
// Increment count
count++;
}
// Update the minimum platforms needed
ans = Math.max(ans, count);
}
}
}
// Return number of platforms
return ans;
}
}
// Main execution
const arr = [900, 945, 955, 1100, 1500, 1800];
const dep = [920, 1200, 1130, 1150, 1900, 2000];
// Creating an instance of Solution class
const sol = new Solution();
// Function call to find minimum number of platforms required
const ans = sol.findPlatform(arr, dep);
console.log("Minimum number of Platforms required:", ans);
Optimally sorting of arrival and departure time
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// To find number of platforms
int findPlatform(vector<int>& Arrival, vector<int>& Departure) {
int n = Arrival.size();
// Sort both arrival and departure arrays
sort(Arrival.begin(), Arrival.end());
sort(Departure.begin(), Departure.end());
int ans = 1;
int count = 1;
int i = 1, j = 0;
// Iterate through the arrays
while (i < n && j < n) {
if (Arrival[i] <= Departure[j]) {
// Increment count
count++;
i++;
} else {
// Decrement Count
count--;
j++;
}
// Find maximum
ans = max(ans, count);
}
return ans;
}
};
int main() {
vector<int> arr = {900, 945, 955, 1100, 1500, 1800};
vector<int> dep = {920, 1200, 1130, 1150, 1900, 2000};
Solution sol;
cout << "Minimum number of Platforms required: " << sol.findPlatform(arr, dep) << endl;
}
import java.util.*;
class Solution {
// To find number of platforms
public int findPlatform(int[] Arrival, int[] Departure) {
int n = Arrival.length;
// Sort both arrival and departure arrays
Arrays.sort(Arrival);
Arrays.sort(Departure);
int ans = 1;
int count = 1;
int i = 1, j = 0;
// Iterate through the arrays
while (i < n && j < n) {
if (Arrival[i] <= Departure[j]) {
// Increment count
count++;
i++;
} else {
// Decrement count
count--;
j++;
}
// Find maximum
ans = Math.max(ans, count);
}
return ans;
}
public static void main(String[] args) {
int[] arr = {900, 945, 955, 1100, 1500, 1800};
int[] dep = {920, 1200, 1130, 1150, 1900, 2000};
Solution sol = new Solution();
System.out.println("Minimum number of Platforms required: " + sol.findPlatform(arr, dep));
}
}
class Solution:
# To find number of platforms
def findPlatform(self, Arrival, Departure):
n = len(Arrival)
# Sort both arrival and departure arrays
Arrival.sort()
Departure.sort()
ans = 1
count = 1
i, j = 1, 0
# Iterate through the arrays
while i < n and j < n:
if Arrival[i] <= Departure[j]:
# Increment count
count += 1
i += 1
else:
# Decrement count
count -= 1
j += 1
# Find maximum
ans = max(ans, count)
return ans
# Test the solution
arr = [900, 945, 955, 1100, 1500, 1800]
dep = [920, 1200, 1130, 1150, 1900, 2000]
solution = Solution()
print("Minimum number of Platforms required:", solution.findPlatform(arr, dep))
class Solution {
// To find number of platforms
findPlatform(Arrival, Departure) {
let n = Arrival.length;
// Sort both arrival and departure arrays
Arrival.sort((a, b) => a - b);
Departure.sort((a, b) => a - b);
let ans = 1;
let count = 1;
let i = 1, j = 0;
// Iterate through the arrays
while (i < n && j < n) {
if (Arrival[i] <= Departure[j]) {
// Increment count
count++;
i++;
} else {
// Decrement count
count--;
j++;
}
// Find maximum
ans = Math.max(ans, count);
}
return ans;
}
}
// Test the solution
let arr = [900, 945, 955, 1100, 1500, 1800];
let dep = [920, 1200, 1130, 1150, 1900, 2000];
let solution = new Solution();
console.log("Minimum number of Platforms required:", solution.findPlatform(arr, dep));
Q: What happens if multiple trains arrive or depart at the same time?
A: If multiple trains arrive at the same time, the platform count increases for each train. Similarly, if multiple trains depart at the same time, the platform count decreases for each departure.
Q: Why sort both arrival and departure arrays?
A: Sorting ensures that trains are processed in chronological order, allowing you to simulate the actual sequence of events and efficiently manage platforms.
Q: What if trains arrive and depart across multiple days?
A: Extend the logic by incorporating the date into the arrival and departure times. Treat the combined date and time as a single timestamp for sorting and processing.
Q: How would you handle cases where arrival and departure times are given in different formats?
A: Standardize the time format before processing. For example, convert all times to 24-hour format or total minutes since midnight.