-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueTime.js
More file actions
28 lines (22 loc) · 746 Bytes
/
queueTime.js
File metadata and controls
28 lines (22 loc) · 746 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
export default function queueTime(customers, numTills) {
if (customers.length === 0) {
return 0;
}
if (numTills === 1) {
return customers.reduce((total, customerTime) => total + customerTime, 0);
}
if (numTills >= customers.length) {
return Math.max(...customers);
}
const customersToCheckout = customers.splice(0, numTills);
const fastestCheckout = Math.min(...customersToCheckout);
const timeRemainingForCheckouts = customersToCheckout.map(
(customerTime) => customerTime - fastestCheckout
);
timeRemainingForCheckouts.reverse().forEach((timeRemaining) => {
if (timeRemaining > 0) {
customers.unshift(timeRemaining);
}
});
return fastestCheckout + queueTime(customers, numTills);
}