📖 Scenario: You are managing a small train station. You want to find out the minimum number of platforms needed so that no train has to wait.You have the arrival and departure times of trains for the day.
🎯 Goal: Build a program that calculates the minimum number of platforms required at the station so that no train waits.
📋 What You'll Learn
Create two arrays called arrival and departure with exact train times.
Create an integer variable called n for the number of trains.
Write a function called findMinimumPlatforms that takes arrival, departure, and n as parameters and returns the minimum number of platforms needed.
Print the minimum number of platforms after calling the function.
💡 Why This Matters
🌍 Real World
Train stations and airports use this logic to manage platforms and gates efficiently to avoid delays.
💼 Career
This problem teaches sorting and two-pointer techniques useful in scheduling, resource allocation, and interval problems in software engineering.
Progress0 / 4 steps
1
Create train arrival and departure arrays
Create two integer arrays called arrival and departure with these exact values: arrival = {900, 940, 950, 1100, 1500, 1800} departure = {910, 1200, 1120, 1130, 1900, 2000}
DSA C++
Hint
Use integer arrays with the exact times given for arrival and departure.
2
Create variable for number of trains
Create an integer variable called n and set it to the number of trains, which is 6.
DSA C++
Hint
Count the number of trains and assign it to n.
3
Write function to find minimum platforms
Write a function called findMinimumPlatforms that takes arrival, departure, and n as parameters and returns an integer. Inside the function: - Sort the arrival and departure arrays. - Use two pointers i and j starting at 0. - Use variables platforms_needed and max_platforms initialized to 0. - While i < n and j < n, if arrival[i] <= departure[j], increment platforms_needed and i, update max_platforms if needed; else decrement platforms_needed and increment j. - Return max_platforms.
DSA C++
Hint
Sort both arrays and use two pointers to count platforms needed.
4
Print the minimum number of platforms
Call the function findMinimumPlatforms with arrival, departure, and n and print the returned value using std::cout.
DSA C++
Hint
Call the function and print the result using std::cout.
Practice
(1/5)
1. What does the "Minimum Number of Platforms" problem primarily ask you to find?
easy
A. The total number of trains passing through the station
B. The maximum number of trains arriving at the station
C. The least number of train platforms needed so no train waits
D. The shortest time a train stays on a platform
Solution
Step 1: Understand the problem goal
The problem asks for the minimum platforms so that no train waits due to platform unavailability.
Step 2: Clarify what is being counted
We count platforms needed to handle overlapping train schedules without delay.
Final Answer:
The least number of train platforms needed so no train waits -> Option C
Quick Check:
Minimum platforms = no waiting trains [OK]
Hint: Focus on overlapping train times to find platform count [OK]
Common Mistakes:
Confusing total trains with platforms needed
Ignoring overlapping arrival and departure times
Thinking about train speed or distance
2. Which of the following C++ code snippets correctly declares vectors to store arrival and departure times for the Minimum Number of Platforms problem?
easy
A. vector<int> arrival; vector<int> departure;
B. vector arrival, departure int;
C. std::vector arrival(int), departure(int);
D. std::vector<int> arrival, departure;
Solution
Step 1: Check correct vector syntax in C++
Vectors must be declared with type inside angle brackets and variable names, e.g., vector<int> arrival;
Step 2: Identify valid declarations
vector<int> arrival; vector<int> departure; uses correct syntax for two separate vectors: vector<int> arrival; vector<int> departure;
Final Answer:
vector<int> arrival; vector<int> departure; -> Option A