-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0875-koko-eating-bananas.cpp
41 lines (34 loc) · 1.03 KB
/
0875-koko-eating-bananas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Given array of banana piles, guards are gone for h hours
Return min int k such that can eat all banans within h
Ex. piles = [3,6,7,11] h = 8 -> 4 (1@3, 2@6, 2@7, 3@11)
Binary search, for each k count hours needed, store min
Time: O(n x log m) -> n = # of piles, m = max # in a pile
Space: O(1)
*/
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int n = piles.size();
int low = 1;
int high = 0;
for (int i = 0; i < n; i++) {
high = max(high, piles[i]);
}
int result = high;
while (low <= high) {
int k = low + (high - low) / 2;
long int hours = 0;
for (int i = 0; i < n; i++) {
hours += ceil((double) piles[i] / k);
}
if (hours <= h) {
result = min(result, k);
high = k - 1;
} else {
low = k + 1;
}
}
return result;
}
};