[Medium] 1701. Average Waiting Time
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 theith customer. The arrival times are sorted in non-decreasing order.time_iis the time needed to prepare the order of theith customer.
When a customer arrives, he gives his order to the chef, and the chef starts preparing it once he is idle. The customer waits until his order is prepared. 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.
Examples
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.00000.
Example 2:
Input: customers = [[5,2],[5,4],[10,3],[20,2]]
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 22, so the waiting time of the fourth customer is 22 - 20 = 2.
So the average waiting time = (2 + 6 + 4 + 2) / 4 = 3.25000.
Constraints
1 <= customers.length <= 10^51 <= arrival_i, time_i <= 10^4arrival_i <= arrival_{i+1}(arrival times are sorted in non-decreasing order)
Thinking Process
- Single Server Queue: Classic queueing theory problem
- Greedy works when local optimal choices lead to global optimum.
- Often sort first to make the greedy choice obvious.
- Prove or sanity-check: would swapping two choices ever help?
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Sort + greedy (this problem) | O(n log n) | O(1) | Interval scheduling, assignment |
| Local greedy choice | O(n) | O(1) | Jump game, gas station |
| Greedy + heap | O(n log n) | O(n) | Merge streams, room allocation |
| Exchange argument | O(n) | O(1) | Prove greedy choice is safe |
Solution
class Solution {
public:
double averageWaitingTime(vector<vector<int>>& customers) {
long long t = 0, totalTime = 0;
for(auto& c: customers) {
int arrival = c[0], order = c[1];
if(t > arrival) {
totalTime += t - arrival;
} else {
t = arrival;
}
totalTime += order;
t += order;
}
return (double) totalTime / customers.size();
}
};
Solution Explanation
Approach: Sort + greedy (this problem)
Key idea: 1. Single Server Queue: Classic queueing theory problem
How the code works:
- Single Server Queue: Classic queueing theory problem
- Greedy works when local optimal choices lead to global optimum.
- Often sort first to make the greedy choice obvious.
- Prove or sanity-check: would swapping two choices ever help?
Walkthrough — input customers = [[1,2],[2,5],[4,3]], expected output 5.00000:
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.00000.
Comparison of Solutions
| Solution | Code Length | Readability | Logic Clarity |
|---|---|---|---|
| Solution 1 | Longer | More explicit | Clear if-else logic |
| Solution 2 | Shorter | More concise | Elegant max() usage |
Common Mistakes
- All customers arrive before chef finishes: Chef always busy
customers = [[1,10],[2,5],[3,3]]- Each customer waits for previous to finish
- Chef always idle: Customers arrive after chef finishes
customers = [[1,2],[5,3],[10,1]]- No waiting time, only order preparation time
- Single customer:
customers = [[1,5]]- Waiting time = order time = 5
- Simultaneous arrivals: Multiple customers arrive at same time
customers = [[5,2],[5,4],[5,3]]- Processed sequentially, later ones wait longer
- Wrong waiting time calculation: Using
start_time - arrivalinstead offinish_time - arrival - Not handling chef idle case: Assuming chef is always busy
- Integer overflow: Not using
long longfor large sums - Wrong order processing: Processing orders out of sequence
- Precision issues: Not using
doublefor division
Related Problems
- LC 1834: Single-Threaded CPU - Similar queue processing with priority
- LC 1882: Process Tasks Using Servers - Multiple servers, task scheduling
- LC 621: Task Scheduler - Task scheduling with cooldown
- LC 253: Meeting Rooms II - Resource allocation, similar simulation
Key Takeaways
- Single Server Queue: Classic queueing theory problem
- Sequential Processing: Orders processed in arrival order
- Waiting Time Formula:
finish_time - arrival_time - Chef Availability:
start_time = max(chef_free_time, arrival_time) - Finish Time:
finish_time = start_time + order_time
References
- LC 1701: Average Waiting Time on LeetCode
- LeetCode Discuss — LC 1701: Average Waiting Time
- LeetCode Editorial (may require premium)