Minimum Number of Platforms in DSA Go - Time & Space Complexity
We want to find out how the time needed to find the minimum number of train platforms grows as the number of trains increases.
How does the program's work change when we have more trains arriving and leaving?
Analyze the time complexity of the following code snippet.
func findPlatform(arrivals []int, departures []int) int {
n := len(arrivals)
sort.Ints(arrivals)
sort.Ints(departures)
i, j, platforms, maxPlatforms := 0, 0, 0, 0
for i < n && j < n {
if arrivals[i] <= departures[j] {
platforms++
if platforms > maxPlatforms {
maxPlatforms = platforms
}
i++
} else {
platforms--
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, and then a single loop comparing elements.
- How many times: Sorting takes time depending on n (number of trains), and the loop runs at most 2n times because each pointer moves through the arrays once.
As the number of trains increases, sorting takes more time, and the loop compares more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 (sorting) + 20 (loop) = ~50 operations |
| 100 | About 100 log 100 + 200 = ~900 operations |
| 1000 | About 1000 log 1000 + 2000 = ~13000 operations |
Pattern observation: The sorting dominates and grows faster than the loop, so total work grows a bit faster than just doubling when input doubles.
Time Complexity: O(n log n)
This means the time needed grows a little faster than the number of trains, mainly because sorting takes more time as trains increase.
[X] Wrong: "The loop alone makes this O(n) time, so the whole code is O(n)."
[OK] Correct: Sorting the arrays takes more time than the loop, so the sorting step controls the total time, making it O(n log n).
Understanding how sorting and single-pass loops combine helps you explain your approach clearly and shows you know how to analyze real problems efficiently.
"What if the arrival and departure times were already sorted? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem context
The problem wants to find how many platforms are needed so that no train has to wait for another to leave.Step 2: Identify the key requirement
We must find the minimum number of platforms so that all trains can be accommodated without delay.Final Answer:
The least number of platforms needed so no train waits -> Option DQuick Check:
Minimum platforms = no waiting trains [OK]
- Confusing total trains with platforms needed
- Ignoring overlapping arrival and departure times
- Thinking about train durations instead of platform count
Solution
Step 1: Recall Go slice syntax
In Go, slices are declared with []int{values}, not with parentheses or braces alone.Step 2: Match correct syntax
arrivals := []int{900, 940, 950}; departures := []int{910, 1200, 1120} uses []int{...} which is correct; others use invalid syntax.Final Answer:
arrivals := []int{900, 940, 950}; departures := []int{910, 1200, 1120} -> Option BQuick Check:
Go slices use []int{...} [OK]
- Using parentheses () instead of braces {}
- Using braces {} without []int type
- Confusing arrays and slices syntax
[900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]?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 overlapping trains
Between 940 and 1120, three trains overlap (940, 950, 1100 arrivals with departures after 940). So minimum platforms = 3.Final Answer:
3 -> Option AQuick Check:
Max overlap = 3 trains [OK]
- Not sorting times before counting
- Counting total trains instead of overlaps
- Mixing arrival and departure times
arrivals := []int{900, 940, 950}
departures := []int{910, 1200, 1120}
i, j, platforms := 0, 0, 0
for i < len(arrivals) && j < len(departures) {
if arrivals[i] <= departures[j] {
platforms++
i++
} else {
platforms--
j++
}
}
fmt.Println(platforms)Solution
Step 1: Understand platform counting logic
When arrival time equals departure time, we should consider the train has left before the next arrives to avoid extra platform count.Step 2: Fix comparison operator
Using <= causes increment even when arrival equals departure, inflating platform count. Changing to < fixes this.Final Answer:
The comparison should be arrivals[i] < departures[j], not <= -> Option CQuick Check:
Use < to avoid counting simultaneous arrival/departure as overlap [OK]
- Using <= instead of < in time comparison
- Incorrect loop termination conditions
- Wrong initialization of platform count
[900, 940, 950, 1100, 1500, 1800] and departure times [910, 1200, 1120, 1130, 1900, 2000]. How would you modify the algorithm to also return the time intervals when the maximum number of platforms are needed?Solution
Step 1: Track platform count and max count during iteration
While iterating sorted arrivals and departures, keep updating current platform count and record when it reaches a new maximum.Step 2: Record time intervals for max platforms
Store the start time when max platforms count is reached and end time when it decreases, to get intervals.Final Answer:
Track intervals when platform count reaches max during iteration -> Option AQuick Check:
Record max platform count intervals while counting platforms [OK]
- Ignoring departure times when tracking intervals
- Only sorting arrivals without departures
- Using total trains count instead of overlaps
Ready to prove it?
Pass the test and this pattern moves unknown
