Average Waiting Time (LeetCode 1701): Solution and Explanation
One chef cooks orders in the order customers arrive. Given each customer's arrival time and cooking time, return the average time a customer waits from arrival until their food is ready.
Intuition
There is one chef and the order of service is fixed, so the whole schedule is decided by a single number: the moment the chef becomes free.
When a customer arrives, two things can happen. If the chef is still busy, the order starts when the chef frees up. If the chef is already idle, the order starts at the arrival time. Either way the start is max(free, arrival).
The order finishes time units later, and that finish time is also when the chef is free for the next customer. The customer's wait is finish - arrival, which already counts both the time spent in the queue and the cooking time.
Summing those waits in one pass and dividing by n gives the answer.
Approach: Single-pass simulation
- Set
clock = 0(when the chef is free) andtotal = 0. - For each
[arrival, time]in order, setclock = max(clock, arrival) + time. - Add
clock - arrivaltototal. - Return
total / nas a floating-point value.
Step-by-step example
customers = [[1,2],[2,5],[4,3]]
- Customer 0 arrives at 1. The chef is free, so cooking runs from 1 to 3. Wait = 3 - 1 = 2, total = 2.
- Customer 1 arrives at 2 while the chef is busy until 3. Cooking runs from 3 to 8. Wait = 8 - 2 = 6, total = 8.
- Customer 2 arrives at 4 while the chef is busy until 8. Cooking runs from 8 to 11. Wait = 11 - 4 = 7, total = 15.
- The average is 15 / 3 = 5.0.
Code: Python, Java, C++
1class Solution:
2 def averageWaitingTime(self, customers: List[List[int]]) -> float:
3 clock = 0
4 total = 0
5 for arrival, time in customers:
6 clock = max(clock, arrival) + time
7 total += clock - arrival
8 return total / len(customers)1class Solution {
2 public double averageWaitingTime(int[][] customers) {
3 long clock = 0;
4 long total = 0;
5 for (int[] c : customers) {
6 clock = Math.max(clock, c[0]) + c[1];
7 total += clock - c[0];
8 }
9 return (double) total / customers.length;
10 }
11}1class Solution {
2public:
3 double averageWaitingTime(vector<vector<int>>& customers) {
4 long long clock = 0;
5 long long total = 0;
6 for (auto& c : customers) {
7 clock = max(clock, (long long)c[0]) + c[1];
8 total += clock - c[0];
9 }
10 return (double)total / customers.size();
11 }
12};Time and space complexity
O(n)
Each customer is handled once with a max, an addition and a subtraction.
O(1)
Only the chef's free time and the running total are stored.
Other approaches
Event queue simulation (time O(n log n), space O(n))
Push arrivals and finishes into a priority queue and process events in time order. It models the kitchen faithfully, but with one chef and a fixed order the queue adds a log factor and code for no benefit.
Store start and finish arrays (time O(n), space O(n))
Build arrays of start and finish times first, then sum finish[i] - arrival[i]. Same idea, but it keeps O(n) extra memory that the running variables make unnecessary.
Common mistakes
- Counting only the time spent in the queue. The wait runs until the order is finished, so it includes the cooking time.
- Starting the order at
clockeven when the customer has not arrived yet. If the chef is idle, the order starts at the arrival time. - Using a 32-bit total. With 10^5 customers the total wait can reach about 5 * 10^13, so use a 64-bit integer.
- Integer division in Java, C++, C# or Go. Cast to a floating-point type before dividing.
Frequently asked questions
Why is a single variable enough to model the kitchen?
Orders are cooked one at a time in arrival order, so the only thing the next customer depends on is when the chef finishes the previous order.
Does the chef ever go idle?
Yes. If a customer arrives after the chef is free, the chef waits for them. The max in max(clock, arrival) handles that gap.
Can the total overflow?
A 32-bit integer can overflow, since waits grow with the queue. A 64-bit total is safe for every input allowed by the constraints.