-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Siddharthrajrana
wants to merge
6
commits into
TheAlgorithms:master
from
Siddharthrajrana:greedy_methods
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8ddef8
Added minimum platform required problem
81c2976
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fb5b89c
ruff --fix in minimum_platforms_required.py
afe3ce5
Resolved Conflicts
a952c18
Fixed ruff checks
4fbe404
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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