-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageWaitingTime.cpp
More file actions
35 lines (30 loc) · 1012 Bytes
/
averageWaitingTime.cpp
File metadata and controls
35 lines (30 loc) · 1012 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
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
double averageWaitingTime(vector<vector<int>> &customers)
{
double sum = 0;
double average;
double currentTime = 0; // Current time the chef has worked till now
double arrivalTime;
double cookingTime; // How long it takes to make the food
for (vector<int> vec : customers)
{
//! splitting the 2d vector to two separate variables since customers vector is a 2d vector
arrivalTime = vec[0];
cookingTime = vec[1]; // Get the cooking time for the customer's food
// If the chef is free before the customer arrives, wait till the customer arrives
if (currentTime < arrivalTime)
{
currentTime = arrivalTime;
}
currentTime += cookingTime; // Add the cooking time to the current time
sum += (currentTime - arrivalTime); // the waiting time for this customer
}
average = sum / customers.size();
return average;
}
};