Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active June 11, 2020 05:07
Show Gist options
  • Save manojnaidu619/e1f26447691a555f2c6f140f1d68fd3a to your computer and use it in GitHub Desktop.
Save manojnaidu619/e1f26447691a555f2c6f140f1d68fd3a to your computer and use it in GitHub Desktop.
Minimum number of platforms needed
# Problem statement is here -> https://www.geeksforgeeks.org/minimum-number-platforms-required-railwaybus-station/
# Assuming the schedule is already sorted!
arrival = [900, 940, 950, 1100, 1500, 1800]
departure = [910, 1200, 1120, 1130, 1900, 2000]
# Uncomment these two lines if schedules are not already sorted
#arrival.sort()
#departure.sort()
arr, dep = 1, 0
platforms = 1
while arr < len(arrival):
if arrival[arr] <= departure[dep]: platforms +=1
arr+=1
dep+=1
if platforms != 1: totalplatforms = platforms-1
print(totalplatforms)
# Time complexity if schedules are sorted -> O(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment