Minimum Number of Platforms in DSA Python - Time & Space Complexity
We want to find out how the time needed to find the minimum number of train platforms changes as the number of trains increases.
How does the program's work grow when we have more trains arriving and leaving?
Analyze the time complexity of the following code snippet.
def find_min_platforms(arrivals, departures):
arrivals.sort()
departures.sort()
i = j = 0
platforms_needed = 0
max_platforms = 0
while i < len(arrivals) and j < len(departures):
if arrivals[i] <= departures[j]:
platforms_needed += 1
max_platforms = max(max_platforms, platforms_needed)
i += 1
else:
platforms_needed -= 1
j += 1
return max_platforms
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 lists, and then a single while loop traversing both lists.
- How many times: Sorting takes about n log n steps for n trains; the while loop runs at most 2n times because it moves through both lists once.
As the number of trains increases, sorting takes more time, growing faster than just going through the lists.
| 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: The sorting part grows faster than the traversal, so it dominates the total time.
Time Complexity: O(n log n)
This means the time needed grows a bit faster than the number of trains because of sorting, but the rest grows linearly.
[X] Wrong: "The while loop alone makes this O(n) time."
[OK] Correct: Sorting the arrival and departure times takes more time than the loop, so the total time is not just linear.
Understanding this time complexity helps you explain how sorting affects performance and why careful traversal after sorting is efficient.
"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 AQuick 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
arrivals for the Minimum Number of Platforms problem?Solution
Step 1: Recall Python list sorting method
Python lists have a method calledsort()that sorts the list in place.Step 2: Check each option
arrivals.sort() usesarrivals.sort()which is correct. sort(arrivals) is invalid syntax. arrivals.sorted() uses a non-existent method. arrivals.sort(arrivals) incorrectly passes the list as argument.Final Answer:
arrivals.sort() -> Option CQuick Check:
List.sort() is correct method [OK]
- Using sort() as a standalone function
- Trying to call sorted() as a method
- Passing the list as argument to sort()
arrivals = [900, 940, 950, 1100, 1500, 1800]
departures = [910, 1200, 1120, 1130, 1900, 2000]Solution
Step 1: Sort arrival and departure times
Arrivals sorted: [900, 940, 950, 1100, 1500, 1800]
Departures sorted: [910, 1120, 1130, 1200, 1900, 2000]Step 2: Use two pointers to count platforms
Start with i=0 (arrivals), j=0 (departures), platforms=0, max_platforms=0.
At 900 (arrival), platforms=1.
At 910 (departure), platforms=0.
At 940 (arrival), platforms=1.
At 950 (arrival), platforms=2.
At 1100 (arrival), platforms=2 (since departure at 1120 is after 1100 but 1130 and 1200 are later).
Max platforms needed = 2.Final Answer:
2 -> Option BQuick Check:
Max overlapping trains = 2 [OK]
- Not sorting times before counting
- Mixing arrival and departure pointers
- Counting platforms incorrectly at overlaps
arrivals = [900, 940, 950]
departures = [910, 1200, 1120]
arrivals.sort()
departures.sort()
i = j = 0
platforms = max_platforms = 0
while i < len(arrivals) and j < len(departures):
if arrivals[i] <= departures[j]:
platforms += 1
i += 1
else:
platforms -= 1
j += 1
max_platforms = max(max_platforms, platforms)
print(max_platforms)
Solution
Step 1: Understand the comparison logic
Using <= means if arrival time equals departure time, it counts as needing a new platform, but usually a train leaving at time t frees the platform for a train arriving at time t.Step 2: Correct the comparison
Changing <= to < ensures that if arrival equals departure, platform count decreases first, avoiding unnecessary extra platform.Final Answer:
The comparison should be arrivals[i] < departures[j], not <=. -> Option AQuick Check:
Use < to avoid counting simultaneous arrival and departure as overlap [OK]
- Using <= causes overcounting platforms
- Changing loop condition wrongly to 'or'
- Not sorting departure times
arrivals = [900, 940, 950, 1100, 1500, 1800, 1800]
departures = [910, 1200, 1120, 1130, 1900, 2000, 1800]Choose the best approach.
Solution
Step 1: Sort both arrival and departure lists
Sorting helps to process events in chronological order, which is essential for counting overlapping trains.Step 2: Use two pointers with strict comparison
Use two pointers to traverse arrivals and departures. Usearrivals[i] < departures[j]to decide when to increase or decrease platform count, handling same-time arrivals and departures correctly.Final Answer:
Sort arrivals and departures, then use two pointers with strict less than comparison for arrival vs departure. -> Option DQuick Check:
Sorting + two pointers + < comparison = correct approach [OK]
- Ignoring sorting of departures
- Using <= causing overcount
- Trying to count without sorting or pointers
Ready to prove it?
Pass the test and this pattern moves unknown
