forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added minimum waiting time problem solution using greedy algorithm (T…
…heAlgorithms#8701) * Added minimum waiting time problem solution using greedy algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff --fix * Add type hints * Added two more doc test * Removed unnecessary comments * updated type hints * Updated the code as per the code review --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f6df26b
commit e966c5c
Showing
2 changed files
with
49 additions
and
0 deletions.
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
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,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. | ||
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 | ||
""" | ||
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() |