Maximum trains for which stoppage can be provided
Pattern: Activity selection
Platforms do not affect each other, so solve each platform on its own: it is activity selection. Sort all trains by departure and let a train stop if its platform is free, that is, if it arrives at or after the last departure there.
The problem
There are n platforms and some trains, each [arrival, departure, platform]. A train may stop only at its own
platform, and only when no other stopping train is there; a train may arrive exactly when the previous one leaves.
Return the most trains that can stop. With the trains above and n = 3 the answer is 5.
Think first
- Do trains on different platforms interact?
- For one platform, which problem is this?
- What does sorting by departure give you?
Hints
Brute force, and why it fails
Try every subset of trains per platform: exponential.
The key insight
Each platform is an independent activity-selection problem, and one sort by departure serves all of them at once.
Dry run
| free | - - - |
|---|
3 platforms. Trains [arrive, depart, platform]: [1000, 1030, 1], [1010, 1030, 1], [1000, 1020, 2], [1030, 1230, 2], [1200, 1230, 3], [900, 1005, 1].
The plan
sort trains by departure
free[p] = -infinity for each platform
for (a, d, p): if a >= free[p]: count += 1; free[p] = d
Complexity
- Time
O(m log m)- Space
O(m + n)
One sort of the m trains, then one pass.
Edge cases
- Arrival equal to the last departure: it stops.
- A platform with no trains.
- One train.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int maxStop(int[][] trains, int n) {
int[][] byDeparture = trains.clone();
Arrays.sort(byDeparture, (a, b) -> Integer.compare(a[1], b[1])); // earliest departure first
int[] free = new int[n + 1]; // free[p]: when platform p is next free
Arrays.fill(free, Integer.MIN_VALUE);
int count = 0;
for (int[] t : byDeparture) {
if (t[0] >= free[t[2]]) { // arriving as the last one leaves is allowed
count++;
free[t[2]] = t[1];
}
}
return count;
}
}C++
class Solution {
public:
int maxStop(vector<vector<int>>& trains, int n) {
vector<vector<int>> byDeparture(trains);
sort(byDeparture.begin(), byDeparture.end(), [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; });
vector<long long> freeAt(n + 1, LLONG_MIN); // freeAt[p]: when platform p is next free
int count = 0;
for (const vector<int>& t : byDeparture) { // earliest departure first
if (t[0] >= freeAt[t[2]]) { // arriving as the last one leaves is allowed
count++;
freeAt[t[2]] = t[1];
}
}
return count;
}
};Python
class Solution:
def maxStop(self, trains: list[list[int]], n: int) -> int:
free = [float("-inf")] * (n + 1) # free[p]: when platform p is next free
count = 0
for arrive, depart, p in sorted(trains, key=lambda t: t[1]): # earliest departure first
if arrive >= free[p]: # arriving as the last one leaves is allowed
count += 1
free[p] = depart
return countCommon mistakes
- Forgetting that platforms are separate and treating all trains as one room.
- Using
>and refusing trains that arrive as another leaves.