Last active
September 5, 2021 22:17
-
-
Save codecakes/3d12a18b938e8481b961ba2bac65d49a to your computer and use it in GitHub Desktop.
Optimal Queue Arrangement - less waiting time
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
# you can sort one time queue like below | |
# you can sort dynamic queue using priority queue - for larger n | |
def waiting_time(**n: dict): | |
"""Optimal queue wait time. | |
>>> waiting_time(t1=15, t2=20, t3=10) | |
35 | |
""" | |
waiting_time = 0 | |
wait_list = {} | |
min_wait_time = 0 | |
sorted_queue = sorted(n, key=lambda patient: n[patient]) | |
for patient in sorted_queue: | |
treat_time = n[patient] | |
wait_list[patient] = waiting_time | |
min_wait_time += waiting_time | |
waiting_time += treat_time | |
return min_wait_time | |
import heapq as heap | |
def waiting_time(wait_times_list: list, idx = 0) -> int: | |
"""Optimal queue wait time. | |
>>> waiting_time(t1=15, t2=20, t3=10) | |
35 | |
""" | |
if not wait_times_list: | |
return 0 | |
idx = idx or 0 | |
if not idx: | |
heap.heapify(wait_times_list) | |
min_time_patient = heap.heappop(wait_times_list) | |
new_idx = idx+1 | |
print('wait_times_list', wait_times_list) | |
if not idx: | |
return waiting_time(wait_times_list, idx = new_idx) | |
return min_time_patient + waiting_time(wait_times_list, idx = new_idx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment