john sokpo

Generation

generate functionThu, 06 Jul 2023

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.

function averageWaitTime($customers) { $time = 0; $prev = 0; $averageWaitTime = 0; for($i = 0; $i < count($customers); $i++) { // If there are other customers that arrive before the current one $customerArr = $customers[$i][0]; // The time needed to prepare the current order $customerTime = $customers[$i][1]; // If the current customer arrived first, he waits until the chef is done preparing the order of the previous customer if($customerArr < $prev) { $averageWaitTime += $prev - $customerArr; } // The time needed to prepare the current order $time = $customerArr + $customerTime; // The time the chef is done preparing the current order $prev = $time; } return $averageWaitTime / count($customers); }

Want to kickstart your project?Use the new AI Studio to create your code