Problem Description
There is a restaurant with a single chef. Customers arrive with an arrival time and a required preparation time. The chef processes orders one at a time in the order they are given. For each customer, the waiting time is the difference between the time when their order is completed and their arrival time. The task is to compute the average waiting time for all customers.
Key Insights
- The customers are processed in the exact order they appear in the input.
- Simulate the order processing by tracking the current time (i.e., when the chef is available).
- If the chef is idle when a customer arrives, the current time is updated to the customer's arrival time.
- Waiting time for each customer is computed as (current time after processing - arrival time).
Space and Time Complexity
Time Complexity: O(n), where n is the number of customers. Space Complexity: O(1) additional space (ignoring the input data).
Solution
We iterate through each customer, and for each, update the chef's current time. If the chef is busy, the customer must wait; if the chef is idle, we move forward to the customer's arrival time. For every customer, we add the wait time, which is the difference between the time the order is completed and the arrival time. Finally, we compute the average waiting time by dividing the total waiting time by the number of customers.