> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Koko Eating Bananas Python Solution with Tests

> Tested Python solution for LeetCode 875 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 875, Medium. Topics: Array, Binary Search. [View on LeetCode](https://leetcode.com/problems/koko-eating-bananas/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 875   # by problem number
lcpy gen -s koko_eating_bananas   # by problem name
```

## Problem

Koko loves to eat bananas. There are `n` piles of bananas, the `i<sup>th</sup>` 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*.

### Examples

```
Input: piles = [3,6,7,11], h = 8
Output: 4
```

```
Input: piles = [30,11,23,4,20], h = 5
Output: 30
```

```
Input: piles = [30,11,23,4,20], h = 6
Output: 23
```

### Constraints

* 1 \<= piles.length \<= 10\<sup>4\</sup>
* piles.length \<= h \<= 10\<sup>9\</sup>
* 1 \<= piles\[i] \<= 10\<sup>9\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/koko_eating_bananas/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/koko_eating_bananas/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import math


class Solution:
    # Time: O(n log m) where n = piles, m = max(piles)
    # Space: O(1)
    def min_eating_speed(self, piles: list[int], h: int) -> int:
        left, right = 1, max(piles)
        result = right
        while left <= right:
            speed = (left + right) // 2
            hours_needed = sum(math.ceil(pile / speed) for pile in piles)
            if hours_needed <= h:
                result = speed  # speed works, try slower
                right = speed - 1
            else:
                left = speed + 1  # too slow, eat faster
        return result
```

## Complexity

| Time                                       | Space |
| ------------------------------------------ | ----- |
| O(n log m) where n = piles, m = max(piles) | O(1)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
