Skip to content

Commit 2482797

Browse files
authored
Create koko-eating-bananas.cpp
1 parent 85a612b commit 2482797

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

C++/koko-eating-bananas.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time: O(nlogr)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minEatingSpeed(vector<int>& piles, int H) {
7+
int left = 1, right = *max_element(piles.cbegin(), piles.cend());
8+
while (left <= right) {
9+
const auto mid = left + (right - left) / 2;
10+
if (possible(piles, H, mid)) {
11+
right = mid - 1;
12+
} else {
13+
left = mid + 1;
14+
}
15+
}
16+
return left;
17+
}
18+
19+
private:
20+
bool possible(const vector<int>& piles, int H, int K) {
21+
int time = 0;
22+
for (const auto& pile : piles) {
23+
time += (pile - 1) / K + 1;
24+
}
25+
return time <= H;
26+
}
27+
};

0 commit comments

Comments
 (0)