Skip to content

Commit bc629a3

Browse files
Solve 9th July problem
1 parent 833211a commit bc629a3

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Intuition
2+
To solve the problem of calculating the average waiting time for customers in a restaurant, we use a simple iterative approach. The key steps involve simulating the process of serving each customer and calculating the total waiting time.
3+
4+
Approach
5+
Input Handling:
6+
7+
customers is a 2D array where each element is an array representing a customer's arrival time and cooking time.
8+
Initialization:
9+
10+
N stores the number of customers.
11+
serviceTime is initialized to the sum of the arrival time and cooking time of the first customer.
12+
totalWait is initialized to the waiting time of the first customer.
13+
Loop Through Customers:
14+
15+
For each customer from the second one onwards:
16+
Check if the chef needs to wait for the customer to arrive.
17+
Update serviceTime to include the cooking time of the current customer.
18+
Calculate the waiting time for the current customer and add it to totalWait.
19+
Calculate and Return Average:
20+
21+
Divide totalWait by N to get the average waiting time.
22+
Complexity
23+
Time complexity: O(N) [N = Row size]
24+
Space complexity: 0(1)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution
2+
{
3+
public:
4+
double averageWaitingTime(vector<vector<int>> &customers)
5+
{
6+
int N = customers.size();
7+
double serviceTime = customers[0][0] + customers[0][1];
8+
double totalWait = serviceTime - customers[0][0];
9+
10+
for (int i = 1; i < N; i++)
11+
{
12+
if (serviceTime < customers[i][0])
13+
serviceTime = customers[i][0];
14+
serviceTime += customers[i][1];
15+
totalWait += (serviceTime - customers[i][0]);
16+
}
17+
return totalWait / N;
18+
}
19+
};

0 commit comments

Comments
 (0)