# 875. Koko Eating Bananas

## Problem Statement

<br>

Koko loves to eat bananas. There are `n` piles of bananas, the `ith` pile has `piles[i]` bananas. The guards have gone and will come back in `h` hours.

Koko can decide her bananas-per-hour eating speed of `k`. Each hour, she chooses some pile of bananas and eats `k` bananas from that pile. If the pile has less than `k` bananas, she eats all of them instead and will not eat any more bananas during this hour.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

Return *the minimum integer* `k` *such that she can eat all the bananas within* `h` *hours*.

&#x20;

**Example 1:**

<pre><code><strong>Input: piles = [3,6,7,11], h = 8
</strong><strong>Output: 4
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: piles = [30,11,23,4,20], h = 5
</strong><strong>Output: 30
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: piles = [30,11,23,4,20], h = 6
</strong><strong>Output: 23
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= piles.length <= 104`
* `piles.length <= h <= 109`
* `1 <= piles[i] <= 109`

## Intuition

```
Approach:
He can eat a maximum of max(array) bananas
range is 1->max(array)

Do BS on this answer space to find the min
```

### Links

<https://leetcode.com/problems/koko-eating-bananas/description/>

### Video Links

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {        
        int size=piles.size(), ans, max=-1;
        
        for(int i=0;i<size;i++){
            if(max<piles[i])
                max=piles[i];
        }

        if(size==h) return max;
        if(size==1){
            return ceil(piles[0]/double(h));
        }

        int low=1, high=max;

        while(low<=high){
            int mid=low+(high-low)/2;
            double count=0;

            for(int i=0; i<size; i++){
                // Replacement for ceil function
                count += (piles[i] + mid -1)/mid;
            }

            if(count>h){
                low=mid+1;
            }
            else { //count<=h
                high=mid-1;
                ans = mid;
            }
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/binary-search/bs-over-answer-space-answer-space/875.-koko-eating-bananas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
