-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-average-waiting-time.py
More file actions
38 lines (33 loc) · 1.61 KB
/
minimum-average-waiting-time.py
File metadata and controls
38 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from heapq import heappop, heappush
N = int(input())
customers = []
for _ in range(N):
# customer[0] is arrival time, customer[1] is wait time
customers.append(tuple(map(int, input().split(' '))))
# Sort by arrival time and then by wait time
customers = sorted(customers)
work_queue = []
sum_wait_time = 0
current_time = 0
# Execute if the customer list is not empty ot work queue is not empty
while customers or work_queue:
# Loop to add all customers who have arrived at or before current time
while customers and customers[0][0] <= current_time:
# Add customer to work queue with the heap sorted using wait time (hence reversing the customer object)
heappush(work_queue, customers.pop(0)[::-1])
if work_queue:
# If work queue is not empty, then get the customer with minimum wait time and serve him
current_customer = heappop(work_queue)
# Current time is updated by the adding the required wait time for serving
current_time += current_customer[0]
# Adding the total wait which the customer waited and add it to the sum
sum_wait_time += current_time - current_customer[1]
else:
# If work queue is empty, then find the next customer who is arriving and add him to the work queue
# If multiple customers arrive at the same time, then the one with the smaller wait time is taken
# This order is maintained by the initial sort
customer = customers.pop(0)[::-1]
heappush(work_queue, customer)
# Current time is updated to customers arrival time
current_time = customer[1]
print(sum_wait_time // N)