Skip to content

Added minimum platforms required problem #9120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions greedy_methods/minimum_platforms_required.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
This is a pure Python implementation of the greedy algorithm
reference: https://practice.geeksforgeeks.org/problems/minimum-platforms-1587115620/1#

For doctests run following command:
python3 -m doctest -v minimum_platforms_required.py

We will sort both arrays. When there is sorted events, it will be easy to maintain
the count of trains that have arrived but not departed. The total platforms needed
at one time can be found by taking the difference between arrivals and departures
minimum will be the final answer.
"""


def minimum_platforms_required(arrival_times, departure_times):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: minimum_platforms_required. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: arrival_times

Please provide type hint for the parameter: departure_times

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return type of this fucntion.

"""Function to count the minimum number of platforms required.

Args:
arrival_times (list): List of arrival times.
departure_times (list): List of departure times.

Returns:
int: Minimum number of platforms required.

Examples:
>>> minimum_platforms_required([900, 945, 955, 1100, 1500, 1800],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more edge and unique doctests.

... [920, 1200, 1130, 1150, 1900, 2000])
3
"""
arrival_times.sort()
departure_times.sort()

platforms_needed = 1
count = 1
i = 1
j = 0

while i < len(arrival_times) and j < len(departure_times):
if arrival_times[i] <= departure_times[j]: # one more platform needed
count += 1
i += 1
else: # one platform can be reduced
count -= 1
j += 1
platforms_needed = max(platforms_needed, count)

return platforms_needed


if __name__ == "__main__":
import doctest

doctest.testmod()