-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1701.AverageWaitingTime.cpp
More file actions
37 lines (31 loc) · 990 Bytes
/
1701.AverageWaitingTime.cpp
File metadata and controls
37 lines (31 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
double averageWaitingTime(vector<vector<int>>& customers) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Calculation variables.
const int n = customers.size();
const double avgMultiplier = 1.0 / (double)n;
// Calculate average wait time.
double avgWait = (double)customers[0][1] * avgMultiplier;
int lastEnd = customers[0][0] + customers[0][1]; // First customer doesnt wait.
for (int i = 1; i < n; i++) {
const int
arrivalTime = customers[i][0],
prepTime = customers[i][1];
// The customer's minimum wait time.
int wait = prepTime;
// Check if customer had to wait for the prep to start.
if (lastEnd > arrivalTime)
wait += lastEnd - arrivalTime;
// Add to average.
avgWait += (double)wait * avgMultiplier;
// Update last end time.
if (arrivalTime > lastEnd) lastEnd = arrivalTime + prepTime;
else lastEnd += prepTime;
}
return avgWait;
}
};