---
tags: leetcode
---
# [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/)
---
# My Solution
## The Key Idea for Solving This Coding Question
## C++ Code
```cpp=
class Solution {
public:
int minEatingSpeed(vector<int> &piles, int h) {
int left = 1, right = 1000000000;
while (left < right) {
int middle = left + (right - left) / 2;
if (isPossible(piles, middle , h)) {
// Koko eats too fast.
right = middle;
} else {
left = middle + 1;
}
}
return left;
}
private:
bool isPossible(vector<int> &piles, int k, int h) {
int time = 0;
for (auto &bananas : piles) {
time = time + bananas / k;
if (bananas % k) {
++time;
}
}
return time <= h;
}
};
```
## Time Complexity
$O(n \cdot \log m)$
$n$ is the length of `piles`.
$m$ is the maximum number of bananas in a singile pile from `piles`.
## Space Complexity
$O(1)$
# Miscellane
<!--
# Test Cases
```
[3,6,7,11]
8
```
```
[30,11,23,4,20]
5
```
```
[30,11,23,4,20]
6
```
* Runtime Error
```
[312884470]
968709470
```
* Wrong Answer
https://leetcode.com/problems/koko-eating-bananas/submissions/866697367/
-->