We want to find the smallest number of train platforms needed so that no train has to wait. We do this by checking how many trains overlap in time.
Analogy: Imagine a parking lot where cars arrive and leave at different times. The minimum number of parking spots needed is the maximum number of cars parked at the same time.
Sort arrival and departure times to compare in order.
while (i < n && j < n) {
Traverse both arrays to check overlapping trains.
if (arrivals[i] <= departures[j]) {
If next train arrives before or at the same time as a train departs, increase platform count.
platform_needed++;
Add one platform for the arriving train.
if (platform_needed > max_platforms) { max_platforms = platform_needed; }
Update max platforms if current needed is higher.
i++;
Move to next arrival.
} else { platform_needed--; j++; }
If train departs before next arrival, free a platform and move departure pointer.
OutputSuccess
Minimum number of platforms needed: 3
Complexity Analysis
Time: O(n log n) because we sort the arrival and departure arrays, then traverse them once.
Space: O(1) because we use only a few extra variables, sorting done in place.
vs Alternative: Naive approach checks every train against every other train, O(n^2), which is slower than sorting and linear traversal.
Edge Cases
No trains (empty arrays)
Returns 0 platforms needed.
DSA C++
while (i < n && j < n) {
All trains arrive and depart at the same time
Platforms needed equals number of trains.
DSA C++
if (arrivals[i] <= departures[j]) {
Trains arrive after all others depart (no overlap)
Only 1 platform needed.
DSA C++
else { platform_needed--; j++; }
When to Use This Pattern
When you see a problem asking for minimum resources to handle overlapping intervals, reach for the 'Minimum Number of Platforms' pattern because sorting and two-pointer traversal efficiently finds maximum overlap.
Common Mistakes
Mistake: Not sorting arrival and departure times separately before processing. Fix: Always sort both arrays independently to correctly compare times in order.
Mistake: Using < instead of <= in comparison, causing incorrect platform count when arrival equals departure. Fix: Use less than or equal (<=) to count arrival before or at departure correctly.
Summary
Finds the minimum number of platforms needed so no train waits by counting overlapping trains.
Use when scheduling resources for intervals that may overlap in time.
Sort arrivals and departures separately, then use two pointers to track overlaps and find max platforms needed.
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