summary
- Calculate the average waiting time of customers in a restaurant.
approach
- This solution uses a greedy approach to calculate the average waiting time of customers
- It iterates over the customers and calculates the waiting time for each customer based on the time the chef is idle
- The total waiting time is then divided by the number of customers to get the average waiting time.
complexity
explain
- The solution works by iterating over the customers and calculating the waiting time for each customer
- If the customer arrives after the chef is idle, the waiting time is the time the chef has been idle plus the preparation time
- If the customer arrives before the chef is idle, the waiting time is the time the chef has been idle plus the preparation time minus the time the customer arrived
- The total waiting time is then divided by the number of customers to get the average waiting time.
Solution Code:
class Solution {
public:
double averageWaitingTime(vector>& customers) {
long long totalWait = 0;
long long time = 0;
for(int i = 0 ; i < customers.size(); i++){
int arrival = customers[i][0];
int prepareTime = customers[i][1];
if (arrival > time){
time = arrival + prepareTime;
totalWait += prepareTime;
} else {
totalWait += time - arrival + prepareTime;
time += prepareTime;
}
}
return (double)totalWait / customers.size();
}
};