We treat arrival and departure events as points on a timeline. By sorting both arrays, we can use two pointers to count how many trains are in the station at any point. When a train arrives, we increase the platform count; when a train departs, we decrease it.
- Sort both
arrivalanddeparturearrays. - Initialize
platforms_needed = 0,max_platforms = 0. - Initialize two pointers:
i = 0(for arrival) andj = 0(for departure). - While
i < n:- If
arrival[i] <= departure[j]:platforms_needed += 1- Update
max_platforms = max(max_platforms, platforms_needed) i += 1
- Else:
platforms_needed -= 1j += 1
- If
- Return
max_platforms.
- Time Complexity: O(N log N) because of sorting.
- Space Complexity: O(1), assuming sorting is in-place or ignoring it.
def min_platforms(arrival, departure):
arrival.sort()
departure.sort()
n = len(arrival)
i, j = 0, 0
platforms_needed = 0
max_platforms = 0
while i < n:
# If train is arriving before or at the time another train departs
if arrival[i] <= departure[j]:
platforms_needed += 1
max_platforms = max(max_platforms, platforms_needed)
i += 1
else:
# Train departed, platform free
platforms_needed -= 1
j += 1
return max_platforms