File tree 2 files changed +43
-0
lines changed
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 354
354
867|[ Transpose Matrix] ( ./0867-transpose-matrix.js ) |Easy|
355
355
868|[ Binary Gap] ( ./0868-binary-gap.js ) |Easy|
356
356
872|[ Leaf-Similar Trees] ( ./0872-leaf-similar-trees.js ) |Easy|
357
+ 875|[ Koko Eating Bananas] ( ./0875-koko-eating-bananas.js ) |Medium|
357
358
876|[ Middle of the Linked List] ( ./0876-middle-of-the-linked-list.js ) |Easy|
358
359
884|[ Uncommon Words from Two Sentences] ( ./0884-uncommon-words-from-two-sentences.js ) |Easy|
359
360
890|[ Find and Replace Pattern] ( ./0890-find-and-replace-pattern.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments