Minimum Number of Platforms in DSA Typescript - Time & Space Complexity
We want to find out how the time needed to calculate the minimum number of train platforms changes as the number of trains increases.
Specifically, how does the code handle more arrival and departure times?
Analyze the time complexity of the following code snippet.
function findMinPlatforms(arrivals: number[], departures: number[]): number {
arrivals.sort((a, b) => a - b);
departures.sort((a, b) => a - b);
let platformNeeded = 0, maxPlatforms = 0;
let i = 0, j = 0;
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] <= departures[j]) {
platformNeeded++;
i++;
if (platformNeeded > maxPlatforms) maxPlatforms = platformNeeded;
} else {
platformNeeded--;
j++;
}
}
return maxPlatforms;
}
This code finds the minimum number of platforms needed so that no train waits, by sorting arrival and departure times and comparing them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the arrival and departure arrays.
- How many times: Each sort runs once on n elements, and the while loop runs at most 2n times.
As the number of trains (n) grows, sorting takes more time, and the loop compares arrivals and departures more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 + 20 = 50 operations |
| 100 | About 100 log 100 + 200 = 1200 operations |
| 1000 | About 1000 log 1000 + 2000 = 13000 operations |
Pattern observation: The operations grow a bit faster than the input size because of sorting, but the main loop grows linearly.
Time Complexity: O(n log n)
This means the time needed grows a little faster than the number of trains because sorting takes more time than just checking each train.
[X] Wrong: "The while loop alone makes this O(n²) because it compares arrivals and departures repeatedly."
[OK] Correct: The loop actually moves forward through both arrays without restarting, so it runs at most 2n times, which is linear, not quadratic.
Understanding how sorting and linear scanning combine helps you explain your approach clearly and shows you can analyze real problems efficiently.
"What if the arrival and departure arrays were already sorted? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of platforms required so that no train has to wait for another to leave.Step 2: Clarify what is being counted
We count overlapping train schedules to find the peak number of trains at the station simultaneously.Final Answer:
The least number of platforms needed so no train waits -> Option DQuick Check:
Minimum platforms = max trains overlapping [OK]
- Confusing total trains with platforms needed
- Ignoring overlapping times
- Counting total arrival or departure times only
Solution
Step 1: Understand sorting numbers in TypeScript
Default sort() treats elements as strings, so numeric sort needs a compare function.Step 2: Identify correct compare function
The function (a, b) => a - b sorts numbers in ascending order correctly.Final Answer:
arrivals.sort((a, b) => a - b); -> Option BQuick Check:
Numeric ascending sort uses (a, b) => a - b [OK]
- Using sort() without compare function for numbers
- Using (a, b) => a + b which is invalid
- Sorting descending when ascending needed
[900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000], what is the minimum number of platforms needed?Solution
Step 1: Sort arrival and departure times
Arrivals: 900, 940, 950, 1100, 1500, 1800; Departures: 910, 1120, 1130, 1200, 1900, 2000.Step 2: Count platforms needed by comparing arrivals and departures
At 900 arrive (1 platform), 910 depart (0), 940 arrive (1), 950 arrive (2), 1100 arrive (3), 1120 depart (2), 1130 depart (1), 1200 depart (0), 1500 arrive (1), 1800 arrive (2), 1900 depart (1), 2000 depart (0). Maximum platforms needed is 4.Final Answer:
4 -> Option CQuick Check:
Max overlapping trains = 4 [OK]
- Not sorting times before processing
- Counting total trains instead of overlaps
- Mixing arrival and departure times
function findPlatforms(arrivals: number[], departures: number[]): number {
arrivals.sort((a, b) => a - b);
departures.sort((a, b) => a - b);
let platformNeeded = 1, maxPlatforms = 1;
let i = 1, j = 0;
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] <= departures[j]) {
platformNeeded++;
i++;
} else {
platformNeeded--;
j++;
}
if (platformNeeded > maxPlatforms) maxPlatforms = platformNeeded;
}
return maxPlatforms;
}Solution
Step 1: Analyze loop initialization
Variable i starts at 1, skipping the first arrival time, which causes missing counting the first train.Step 2: Correct initialization for accurate counting
Starting i at 0 ensures all arrivals are considered, giving correct platform count.Final Answer:
Start i from 0 instead of 1 to include first arrival -> Option AQuick Check:
Include all arrivals by starting i=0 [OK]
- Skipping first arrival by starting i at 1
- Changing comparison operator incorrectly
- Initializing platformNeeded wrongly
[1000, 1010, 1025, 1030, 1040]
Departures: [1015, 1020, 1035, 1045, 1050]
If you want to minimize the number of platforms by rescheduling trains without changing their arrival order, which train's departure time should you delay to reduce platform count?Solution
Step 1: Identify overlapping trains causing max platforms
Trains at 1010 and 1025 overlap because 1010 departs at 1020 and 1025 arrives before that.Step 2: Delay departure of train at 1010 to after 1025 departs
Delaying 1010's departure reduces overlap with 1025, lowering platform need.Final Answer:
Delay the departure of train arriving at 1010 -> Option AQuick Check:
Delay overlapping train's departure to reduce platforms [OK]
- Delaying wrong train's departure
- Changing arrival order which is fixed
- Ignoring overlap timing details
Ready to prove it?
Pass the test and this pattern moves unknown
