Koko Eating Bananas LeetCode solution in JavaScript

1
var minEatingSpeed = function (piles, h) {
2
/*The range of bananas that Koko can eat is k = 1 to Max(piles)*/
3
let startk = 1;
4
let endk = Math.max(...piles);
5

6
while (startk <= endk) {
7
let midk = Math.floor(startk + (endk - startk) / 2);
8
/*midk are the count of bananas that koko decide to eat.
9
So how many hours she will take to finish the piles?*/
10
let hrs = 0;
11
for (let pile of piles) {
12
/*pile is the num of bananas in piles*/
13
hrs += Math.ceil(pile / midk);
14
}
15
if (hrs > h) {
16
/*Now if hrs > h she will not be to finish the pile so we have
17
to increase the bananas by moving start.*/
18
startk = midk + 1;
19
} else {
20
/*If hrs <= h she will be eating too fast so we can reduce the bananas
21
so she eats slowly. So decrement end.*/
22
endk = midk - 1;
23
}
24
}
25
return startk;
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0