Try   HackMD

875. Koko Eating Bananas


My Solution

The Key Idea for Solving This Coding Question

C++ Code

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(nlogm)
n
is the length of piles.
m
is the maximum number of bananas in a singile pile from piles.

Space Complexity

O(1)

Miscellane