Minimum Number of Platforms in DSA Javascript - Time & Space Complexity
We want to find out how the time needed to find the minimum number of platforms changes as the number of trains increases.
Specifically, we ask: How does the running time grow when we have more arrival and departure times?
Analyze the time complexity of the following code snippet.
function findMinPlatforms(arrivals, departures) {
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 then 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 over all trains, so twice total.
- Secondary operation: The while loop traverses both arrays once, comparing elements.
- How many times: Up to 2n steps combined, where n is the number of trains.
Sorting takes more time as the number of trains grows, and the loop also grows linearly with trains.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 + 20 steps |
| 100 | About 100 log 100 + 200 steps |
| 1000 | About 1000 log 1000 + 2000 steps |
Pattern observation: Sorting dominates and grows faster than the linear loop, so total time grows a bit faster than just doubling when input doubles.
Time Complexity: O(n log n)
This means as the number of trains grows, the time to find the minimum platforms grows a bit faster than just the number of trains, mainly because of sorting.
[X] Wrong: "The while loop is the slowest part, so the time complexity is O(n)."
[OK] Correct: Sorting the arrays takes more time than the loop, so the overall time depends on sorting, not just the loop.
Understanding this time complexity helps you explain how sorting and linear scans work together in real problems, a skill useful in many coding challenges.
"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 platforms so that no train waits due to platform unavailability.Step 2: Clarify what is being counted
We count platforms needed simultaneously for overlapping train times, not total trains or times.Final Answer:
The least number of platforms needed so no train waits -> Option AQuick Check:
Minimum platforms = least needed to avoid waiting [OK]
- Confusing total trains with platforms needed
- Thinking about train durations instead of overlaps
- Ignoring simultaneous arrivals and departures
Solution
Step 1: Understand JavaScript sort behavior
By default, sort() converts elements to strings and sorts lexicographically, which is incorrect for numbers.Step 2: Use numeric comparison function
Using (a, b) => a - b sorts numbers in ascending order correctly.Final Answer:
arrivals.sort((a, b) => a - b) -> Option BQuick Check:
Numeric sort uses (a, b) => a - b [OK]
- Using sort() without compare function for numbers
- Using incorrect compare functions like a + b
- Sorting in descending order when ascending 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 arrival, platform count=1; 940 arrival before 910 departure? No, so platform count=1; 950 arrival before 1120 departure? Yes, platform count=2; 1100 arrival before 1120 departure? Yes, platform count=3; Then departures reduce count as trains leave.Final Answer:
3 -> Option DQuick Check:
Max overlapping trains = 3 platforms needed [OK]
- Not sorting times before counting
- Counting total trains instead of overlaps
- Mixing arrival and departure comparisons
let i = 0, j = 0, platforms = 0, maxPlatforms = 0;
arrivals.sort();
departures.sort();
while (i < arrivals.length && j < departures.length) {
if (arrivals[i] <= departures[j]) {
platforms++;
i++;
if (platforms > maxPlatforms) maxPlatforms = platforms;
} else {
platforms--;
j++;
}
}
return maxPlatforms;Solution
Step 1: Check sorting method
arrivals.sort() and departures.sort() sort lexicographically by default, causing wrong order for numbers.Step 2: Correct sorting for numeric arrays
Use arrivals.sort((a,b) => a - b) and departures.sort((a,b) => a - b) to sort numbers properly.Final Answer:
Sorting arrays without numeric compare function -> Option CQuick Check:
Numeric sort needs compare function [OK]
- Sorting numbers as strings causing wrong order
- Confusing comparison operators in if condition
- Incorrect loop increments
Solution
Step 1: Sort arrival and departure times
Arrivals: 1000, 1010, 1025, 1030, 1040; Departures: 1020, 1035, 1045, 1050, 1100.Step 2: Count platforms considering arrival == departure means no conflict
Compare arrivals and departures with condition arrivals[i] < departures[j] (strict less than). Counting overlaps: at 1000 arrival platform=1; 1010 arrival platform=2; 1025 arrival platform=3; 1020 departure reduces platform to 2; 1030 arrival platform=3; 1035 departure reduces to 2; 1040 arrival platform=3; then departures reduce count.Final Answer:
3 -> Option AQuick Check:
Strict less than counts simultaneous arrival/departure correctly [OK]
- Using <= causing extra platform count
- Not sorting times before counting
- Ignoring simultaneous arrival and departure rule
Ready to prove it?
Pass the test and this pattern moves unknown
