Average Waiting Time LeetCode solution in JavaScript

1
/**
2
* @param {number[][]} customers
3
* @return {number}
4
*/
5
var averageWaitingTime = function (customers) {
6
let n = customers.length;
7
let starts = Array(n).fill(0);
8
// the first customer do not have to wait for the prev customer so his start time == arrival time
9
starts[0] = customers[0][0];
10
let totalWait = customers[0][1];
11
for (let i = 1; i < n; i++) {
12
// The start of the current customer is the max of (start + time of prev customer) or the arrival time of existing customer
13
starts[i] = Math.max(starts[i - 1] + customers[i - 1][1], customers[i][0]);
14
totalWait += starts[i] - customers[i][0] + customers[i][1];
15
}
16

17
return totalWait / n;
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0