Average Waiting Time Problem
Description
LeetCode Problem 1701.
There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrival_i, time_i]:
arrival_iis the arrival time of thei^thcustomer. The arrival times are sorted in non-decreasing order.time_iis the time needed to prepare the order of thei^thcustomer.
When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.
Return the average waiting time of all customers. Solutions within 10^(-5) from the actual answer are considered accepted.
Example 1:
Input: customers = [[1,2],[2,5],[4,3]]
Output: 5.00000
Explanation:
1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2.
2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6.
3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7.
So the average waiting time = (2 + 6 + 7) / 3 = 5.
Example 2:
Input: customers = [[5,2],[5,4],[10,3],[20,1]]
Output: 3.25000
Explanation:
1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2.
2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6.
3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4.
4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1.
So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
Constraints:
1 <= customers.length <= 10^51 <= arrival_i, time_i <= 10^4arrival_i<= arrival_(i+1)
Solution
Approach: Simulation
Keep t, the time the chef finishes the current order, and tot, the total waiting time; both start at 0.
For each customer [arrival, time] in order, the chef starts cooking at max(t, arrival): right away if idle, otherwise after the previous orders. So t = max(t, arrival) + time, and this customer waits t - arrival, which is added to tot.
The answer is tot / n. Keep the total in a 64-bit or floating-point variable, since it can go far past 2^31.
Complexity: O(n) time and O(1) space, where n is the number of customers.
class Solution:
def averageWaitingTime(self, customers: List[List[int]]) -> float:
tot = t = 0
for a, b in customers:
t = max(t, a) + b
tot += t - a
return tot / len(customers)