Back to DSA
Koko Eating Bananas
mediumThere are n piles of fruit, with pile i containing piles[i] pieces. You can consume at most k pieces per hour from a single pile; if a pile has fewer than k pieces, you finish it in that hour and wait. Given h hours in total, find the smallest value of k that lets you finish all piles in time.
Examples
Example 1:
Input:
piles = [5,8,12,6], h = 10Output:
5Explanation: At speed 5: ceil(5/5)+ceil(8/5)+ceil(12/5)+ceil(6/5) = 1+2+3+2 = 8 hours, which fits within 10.
Example 2:
Input:
piles = [20,10,15], h = 3Output:
20Explanation: At speed 20, each pile takes exactly 1 hour, totaling 3 hours.
Hints
1234567