[LeetCode] Average Waiting Time

1701. Average Waiting Time

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:

  • arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order.
  • timei is the time needed to prepare the order of the ith customer.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
double averageWaitingTime(vector<vector<int>>& cus) {
long t = 0, sum = 0;
for(auto& c : cus) {
if(c[0] > t) {
t = c[0] + c[1];
sum += c[1];
} else {
t += c[1];
sum += t - c[0];
}
}
return 1.0 * sum / cus.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/31/PS/LeetCode/average-waiting-time/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.