Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/875.html

875. Koko Eating Bananas (Medium)

Koko loves to eat bananas.  There are N piles of bananas, the i-th 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 won’t eat any more bananas during this hour.

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

Return the minimum integer K such that she can eat all the bananas within H hours.

Example 1:

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

Example 2:

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

Example 3:

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

Note:

  • 1 <= piles.length <= 10^4
  • piles.length <= H <= 10^9
  • 1 <= piles[i] <= 10^9

Search a eating speed K between [1, P_max] where P_max is the max pile size. The K should be the first (smallest) one requiring h (h <= H) hour to consume all the bananas.

// OJ: https://leetcode.com/problems/koko-eating-bananas/
// Time: O(P*log(P_max)) where P is count of piles, P_max is max pile size.
// Space: O(K)
class Solution {
private:
    int countH(vector<int> &piles, int K) {
        int cnt = 0;
        for (int p : piles) cnt += (p + K - 1) / K;
        return cnt;
    }
public:
    int minEatingSpeed(vector<int>& piles, int H) {
        int L = 1, R = *max_element(piles.begin(), piles.end());
        while (L <= R) {
            int M = L + (R - L) / 2;
            int h = countH(piles, M);
            if (h > H) L = M + 1;
            else R = M - 1;
        }
        return L;
    }
};
  • class Solution {
        public int minEatingSpeed(int[] piles, int H) {
            long sum = 0;
            for (int pile : piles)
                sum += pile;
            long low = 1, high = sum;
            while (low < high) {
                long speed = (high - low) / 2 + low;
                long time = timeToEat(piles, speed);
                if (time > H)
                    low = speed + 1;
                else
                    high = speed;
            }
            return (int) low;
        }
    
        public long timeToEat(int[] piles, long speed) {
            long time = 0;
            for (int pile : piles) {
                int curTime = (int) Math.ceil(1.0 * pile / speed);
                time += curTime;
            }
            return time;
        }
    }
    
    ############
    
    class Solution {
        public int minEatingSpeed(int[] piles, int h) {
            int left = 1, right = (int) 1e9;
            while (left < right) {
                int mid = (left + right) >>> 1;
                int s = 0;
                for (int x : piles) {
                    s += (x + mid - 1) / mid;
                }
                if (s <= h) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
            return left;
        }
    }
    
  • // OJ: https://leetcode.com/problems/koko-eating-bananas/
    // Time: O(P*log(P_max)) where P is count of piles, P_max is max pile size.
    // Space: O(K)
    class Solution {
    public:
        int minEatingSpeed(vector<int>& A, int h) {
            long L = 1, R = *max_element(begin(A), end(A)), N = A.size();
            auto valid = [&](long k) {
                int i = 0, tmp = h; 
                for (; i < N && h > 0; ++i) {
                    tmp -= (A[i] + k - 1) / k;
                }
                return i == N && tmp >= 0;
            };
            while (L <= R) {
                long M = (L + R) / 2;
                if (valid(M)) R = M - 1;
                else L = M + 1;
            }
            return L;
        }
    };
    
  • class Solution:
        def minEatingSpeed(self, piles: List[int], h: int) -> int:
            left, right = 1, int(1e9)
            while left < right:
                mid = (left + right) >> 1
                s = sum((x + mid - 1) // mid for x in piles)
                if s <= h:
                    right = mid
                else:
                    left = mid + 1
            return left
    
    ############
    
    class Solution:
        def minEatingSpeed(self, piles, H):
            """
            :type piles: List[int]
            :type H: int
            :rtype: int
            """
            minSpeed, maxSpeed = 1, max(piles)
            while minSpeed <= maxSpeed:
                speed = minSpeed + (maxSpeed - minSpeed) // 2
                hour = 0
                for pile in piles:
                    hour += math.ceil(pile / speed)
                if hour <= H:
                    maxSpeed = speed - 1
                else:
                    minSpeed = speed + 1
            return minSpeed
    
  • func minEatingSpeed(piles []int, h int) int {
    	return sort.Search(1e9, func(i int) bool {
    		if i == 0 {
    			return false
    		}
    		s := 0
    		for _, x := range piles {
    			s += (x + i - 1) / i
    		}
    		return s <= h
    	})
    }
    
  • function minEatingSpeed(piles: number[], h: number): number {
        let left = 1;
        let right = Math.max(...piles);
        while (left < right) {
            const mid = (left + right) >> 1;
            let s = 0;
            for (const x of piles) {
                s += Math.ceil(x / mid);
            }
            if (s <= h) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
    
    
  • public class Solution {
        public int MinEatingSpeed(int[] piles, int h) {
            int left = 1, right = piles.Max();
            while (left < right)
            {
                int mid = (left + right) >> 1;
                int s = 0;
                foreach (int pile in piles)
                {
                    s += (pile + mid - 1) / mid;
                }
                if (s <= h)
                {
                    right = mid;
                }
                else
                {
                    left = mid + 1;
                }
            }
            return left;
        }
    }
    

All Problems

All Solutions