Maximum Running Time of N Computers LeetCode solution in JavaScript

1
var maxRunTime = function (n, batteries) {
2
let total = batteries.reduce((acc, x) => acc + x, 0);
3
let batts = batteries.sort((a, b) => b - a);
4
let i = 0;
5
while (1) {
6
let average_truncated = parseInt(total / n);
7
let cur = batts[i];
8
if (cur > average_truncated) {
9
total -= cur; // remove all of that batteries charge from the equation
10
n--; // remove the computer from the equation
11
i++;
12
} else {
13
return average_truncated;
14
}
15
}
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0