-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Minimum platforms issue 9119 #9222
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
dev-soni-07
wants to merge
7
commits into
TheAlgorithms:master
from
dev-soni-07:minimum-platforms-issue-9119
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0b06bba
Make binary tree traversals lazy using generators
dev-soni-07 9a6de56
Feature: Addition of Minimum Number of Platforms (Greedy Algorithm)
dev-soni-07 c0d1e56
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b83826d
Feature: Addition of Minimum Number of Platforms (Greedy Algorithm)
dev-soni-07 513cf50
Merge branch 'minimum-platforms-issue-9119' of https://github.com/dig…
dev-soni-07 e9163ed
Feature: Addition of Minimum Number of Platforms (Greedy Algorithm)
dev-soni-07 4bed322
[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 hidden or 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
This file contains hidden or 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,44 @@ | ||
from typing import List | ||
|
||
|
||
def minimum_platforms(arrival: List[int], departure: List[int]) -> int: | ||
""" | ||
Calculate the minimum number of platforms required at a railway station. | ||
|
||
:param arrival: List of arrival times of trains | ||
:param departure: List of departure times of trains | ||
:return: Minimum number of platforms required | ||
""" | ||
if not arrival or not departure: | ||
return 0 | ||
|
||
arrival.sort() | ||
departure.sort() | ||
|
||
platforms_needed = 1 | ||
result = 1 | ||
i = 1 | ||
j = 0 | ||
|
||
while i < len(arrival) and j < len(departure): | ||
if arrival[i] <= departure[j]: | ||
platforms_needed += 1 | ||
i += 1 | ||
elif arrival[i] > departure[j]: | ||
platforms_needed -= 1 | ||
j += 1 | ||
|
||
if platforms_needed > result: | ||
result = platforms_needed | ||
|
||
return result | ||
|
||
|
||
# Example usage: | ||
arrival_times = [900, 940, 950, 1100, 1500, 1800] | ||
departure_times = [910, 1200, 1120, 1130, 1900, 2000] | ||
|
||
print( | ||
"Minimum number of platforms required:", | ||
minimum_platforms(arrival_times, departure_times), | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.