Problem

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.

Examples

Example 1:

Input: customers = [ [1,2],[2,5],[4,3] ]
Output: 5.00000
Explanation:
1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2.
2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6.
3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7.
So the average waiting time = (2 + 6 + 7) / 3 = 5.

Example 2:

Input:
customers = [ [5,2],[5,4],[10,3],[20,1] ]
Output: 3.25000
Explanation:
1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2.
2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6.
3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4.
4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1.
So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.

Solution

Method 1 - Track Current Time using array

Algorithm

  1. Keep track of currentTime currTime
  2. Create an array endTimes to track when the customer’s job was finished
  3. Each time the chef is already idle, and customer arrives later, we reset currTime to customer’s time.
  4. Calculate the total time for all customers by summing all (endTimes[i] - customers[i][0]).
  5. Finally calculate average

Code

Java
public double averageWaitingTime(int[][] customers) {
	int[] endTimes = new int[customers.length];
	int currTime = 0;
	for(int i = 0; i < customers.length; i++) {
		int[] cust = customers[i];
		// reset currTime if customer arrives after chef is idle
		if(currTime < cust[0]) {
			currTime = cust[0];
		}
		currTime += cust[1];
		endTimes[i] = currTime;
	}
	
	long total = 0;
	for(int i = 0; i < customers.length; i++) {
		total += (endTimes[i] - customers[i][0]);
	}
	return (double)total / (double)customers.length;
}

Complexity

  • ⏰ Time complexity: O(n)
  • 🧺 Space complexity: O(n)

Method 2 - Track Current Time using array without extra space

As we see we dont need endTimes array. We can just calculate on the fly by using variable waitingTime.

Code

Java
public double averageWaitingTime(int[][] customers) {
	double waitingTime = 0;
	int currTime = 0;
	for(int i = 0; i < customers.length; i++) {
		int[] cust = customers[i];
		// reset currTime if customer arrives after chef is idle
		currTime = Math.max(currTime, cust[0]);
		currTime += cust[1];
		waitingTime += (currTime - cust[0]);
	}
	
	return waitingTime / customers.length;
}

Also, note we switched to max to reduce line of code and changed waitingTime to double.

Complexity

  • ⏰ Time complexity: O(n)
  • 🧺 Space complexity: O(1)