Skip to content

Commit 01cc2d5

Browse files
committed
Add solution #875
1 parent 4f69fc3 commit 01cc2d5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@
354354
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
355355
868|[Binary Gap](./0868-binary-gap.js)|Easy|
356356
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|
357+
875|[Koko Eating Bananas](./0875-koko-eating-bananas.js)|Medium|
357358
876|[Middle of the Linked List](./0876-middle-of-the-linked-list.js)|Easy|
358359
884|[Uncommon Words from Two Sentences](./0884-uncommon-words-from-two-sentences.js)|Easy|
359360
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|

solutions/0875-koko-eating-bananas.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 875. Koko Eating Bananas
3+
* https://leetcode.com/problems/koko-eating-bananas/
4+
* Difficulty: Medium
5+
*
6+
* Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas.
7+
* The guards have gone and will come back in h hours.
8+
*
9+
* Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of
10+
* bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats
11+
* all of them instead and will not eat any more bananas during this hour.
12+
*
13+
* Koko likes to eat slowly but still wants to finish eating all the bananas before the guards
14+
* return.
15+
*
16+
* Return the minimum integer k such that she can eat all the bananas within h hours.
17+
*/
18+
19+
/**
20+
* @param {number[]} piles
21+
* @param {number} h
22+
* @return {number}
23+
*/
24+
var minEatingSpeed = function(piles, h) {
25+
const fn = speed => piles.reduce((sum, pile) => sum + Math.ceil(pile / speed), 0);
26+
let min = 1;
27+
let max = Math.max(...piles);
28+
let result = max;
29+
30+
while (min <= max) {
31+
const middle = Math.floor((min + max) / 2);
32+
33+
if (fn(middle) <= h) {
34+
result = middle;
35+
max = middle - 1;
36+
} else {
37+
min = middle + 1;
38+
}
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)