Search⌘ K
AI Features

Solution: Find the Minimum Platforms Required for a Station

Explore how to efficiently determine the minimum number of platforms needed at a railway station through greedy algorithm techniques. Learn to optimize from a brute force approach to a sorting method that reduces time complexity, improving your problem-solving skills for coding interviews.

Solution 1: Brute force

C# 9.0
using System;
class Program
{
/// <summary>
/// Finds the minimum number of platforms required for a railway station.
/// </summary>
/// <param name="arrival">An array of arrival timings.</param>
/// <param name="departure">An array of departure timings.</param>
/// <returns>Minimum number of platforms required for a railway station.</returns>
public static int FindPlatform(int[] arrival, int[] departure)
{
int result = 0;
int count;
int n = arrival.Length;
for (int index = 0; index < n; index++)
{
count = 0;
for (int i = 0; i < n; i++)
{
if (arrival[index] <= arrival[i] && arrival[i] <= departure[index])
{
count++;
}
}
if (result < count)
{
result = count;
}
}
return result;
}
// Driver code to test above method
static void Main(string[] args)
{
int[] arrival1 = { 900, 940, 950, 1100, 1500, 1800 };
int[] departure1 = { 910, 1200, 1120, 1130, 1900, 2000 };
Console.WriteLine(FindPlatform(arrival1, departure1)); // Output: 3
int[] arrival2 = { 200, 210, 300, 320, 350, 500 };
int[] departure2 = { 230, 240, 320, 430, 400, 520 };
Console.WriteLine(FindPlatform(arrival2, departure2)); // Output: 2
}
}

Explanation

The problem is to find the maximum number of trains that are there at the given railway station at a time. An iterative solution would be to take every interval, one by one, and find the number of intervals that overlap with it. We keep track of the maximum number of intervals that overlap with an ...