1701. Average Waiting Time - Explanation
Description
There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrival[i], time[i]]:
arrival[i]is the arrival time of thei-thcustomer. The arrival times are sorted in non-decreasing order.time[i]is 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.00000Explanation:
- 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.
- 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.
- 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.25000Explanation:
- 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.
- 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.
- 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.
- 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 <= arrivali, timei <= 10^4arrival[i] <= arrival[i+1]
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Simulation - Processing events in order and tracking state over time
- Linear Array Traversal - Iterating through customers in arrival order
- Basic Arithmetic - Calculating wait times using max operations and running totals
1. Simulation - I
Intuition
We simulate the chef serving customers one by one. The chef can only start a new order when they finish the previous one. If a customer arrives while the chef is busy, they wait. The waiting time for each customer is the time from arrival until their order is complete. We track the current time and accumulate total waiting time.
Algorithm
- Initialize
t(current time) andtotal(total waiting time) to0. - For each customer with arrival time and order duration:
- If the chef is still busy (
t > arrival), add the extra wait tototal. - Otherwise, update
tto the arrival time. - Add the order time to both
totalandt.
- If the chef is still busy (
- Return
totaldivided by the number of customers.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Simulation - II
Intuition
This is a more concise version of the simulation. The key observation is that the chef starts cooking at whichever is later: their current finish time or the customer's arrival. The finish time becomes max(t, arrival) + order. The customer's waiting time is simply finish_time - arrival. This formula captures both cases (chef idle or busy) in one expression.
Algorithm
- Initialize
t(finish time) andtotalto0. - For each customer:
- Update
ttomax(t, arrival) + order(when the order finishes). - Add
t - arrivaltototal(customer's wait time).
- Update
- Return
totaldivided by the number of customers.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Integer Overflow
With large arrival times and order durations, the sum of waiting times can exceed 32-bit integer limits. Use 64-bit integers for accumulating totals.
// Wrong: int can overflow
int total = 0;
// Right: use long for accumulation
long total = 0;Confusing Waiting Time with Service Time
Waiting time is from arrival until the order is complete (includes cooking time), not just the time spent waiting before cooking starts.
# Wrong: only counting idle wait time
wait = max(0, t - arrival)
# Right: total time from arrival to completion
wait = (max(t, arrival) + order) - arrivalForgetting to Update Current Time
When a customer arrives after the chef is idle, the current time must jump forward to the arrival time, not stay at the previous finish time.
# Wrong: always adding order time to current t
t += order
# Right: chef starts at max(t, arrival)
t = max(t, arrival) + order
Sign in to join the discussion