---
# System prepended metadata

title: 0875. Koko Eating Bananas
tags: [Leetcode, Binary Search, Medium]

---

# 0875. Koko Eating Bananas
###### tags: `Leetcode` `Medium` `Binary Search`
Link: https://leetcode.com/problems/koko-eating-bananas/
## 思路 $O(NlogN)$ $O(1)$ 
N is the maximum pile
## Code
```java=
class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int start = 1;
        int end = 1;
        for(int i = 0;i < piles.length;i++){
            end = Math.max(end, piles[i]);
        }
        while(start<end){
            int mid = start+(end-start)/2;
            if(eatingTime(piles, mid)>h){
                start = mid+1;
            }
            else{
                end = mid;
            }
        }
        return start;
    }
    private int eatingTime(int[] piles, int speed){
        int time = 0;
        for(int pile:piles){
            time += Math.ceil((double)pile/speed);
        }
        return time;
    }
}
```