-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added minimum waiting time problem solution using greedy algorithm #8701
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0b75e10
Added minimum waiting time problem solution using greedy algorithm
Himanshutomar31 6520b7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bd51b5a
ruff --fix
Himanshutomar31 4e9e136
Merge branch 'master' of https://github.com/Himanshutomar31/Python-1
Himanshutomar31 0bd440e
Add type hints
Himanshutomar31 ac29bff
Added two more doc test
Himanshutomar31 68fa2da
Merge branch 'TheAlgorithms:master' into master
Himanshutomar31 8062a3c
Removed unnecessary comments
Himanshutomar31 727d449
Merge branch 'master' of https://github.com/Himanshutomar31/Python-1
Himanshutomar31 27a0d4a
updated type hints
Himanshutomar31 b2bf988
Updated the code as per the code review
Himanshutomar31 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,48 @@ | ||
""" | ||
Calculate the minimum waiting time using a greedy algorithm. | ||
reference: https://www.youtube.com/watch?v=Sf3eiO12eJs | ||
|
||
For doctests run following command: | ||
python -m doctest -v minimum_waiting_time.py | ||
|
||
The minimum_waiting_time function uses a greedy algorithm to calculate the minimum | ||
time for queries to complete. It sorts the list in non-decreasing order, calculates | ||
the waiting time for each query by multiplying its position in the list with the | ||
sum of all remaining query times, and returns the total waiting time. A doctest | ||
ensures that the function produces the correct output. | ||
""" | ||
|
||
|
||
def minimum_waiting_time(queries: list[int]) -> int: | ||
""" | ||
This function takes a list of query times and returns the minimum waiting time | ||
for all queries to be completed. | ||
Himanshutomar31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
queries: A list of queries measured in picoseconds | ||
|
||
Returns: | ||
total_waiting_time: Minimum waiting time measured in picoseconds | ||
|
||
Examples: | ||
>>> minimum_waiting_time([3, 2, 1, 2, 6]) | ||
17 | ||
>>> minimum_waiting_time([3, 2, 1]) | ||
4 | ||
>>> minimum_waiting_time([1, 2, 3, 4]) | ||
10 | ||
>>> minimum_waiting_time([5, 5, 5, 5]) | ||
30 | ||
>>> minimum_waiting_time([]) | ||
0 | ||
""" | ||
Himanshutomar31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n = len(queries) | ||
if n in (0, 1): | ||
return 0 | ||
return sum(query * (n - i - 1) for i, query in enumerate(sorted(queries))) | ||
|
||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.