The 30 second answer
Koko must finish every pile in h hours, eating at a fixed integer speed, and she wants the slowest speed that still works. I binary-search the speed between 1 and the largest pile. For each mid speed I add up the hours, using ceiling division, and I move the search toward the smallest speed whose hours stay within h.
Problem Koko has n piles of bananas and h hours. Each hour she chooses one pile and eats k bananas from it, or the whole pile if fewer than k remain, and she will not start another pile in the same hour. Return the minimum integer k that lets her finish in at most h hours. Original: LeetCode 875: Koko Eating Bananas.
Understanding the prompt k = max(pile) always works, because each pile takes one hour. k = 1 always works given enough hours, but may exceed h. Hours needed is a decreasing function of k, so the feasible speeds form a suffix of 1..max(pile) and you can search for the first feasible k. Division must use a long, and hours must be summed in a long, so large piles do not overflow.
Pattern recognition Binary search on the answer, with a monotonic feasibility check: can she finish at speed k?
Brute force Try every k from 1 to max(pile): O(n * max(pile)).
Optimal lo = 1, hi = max(pile). While lo < hi, mid = lo + (hi - lo) / 2. If the hours needed at speed mid is at most h, a slower speed might still work, so hi = mid; otherwise lo = mid + 1. Hours for a pile p at speed k are (p + k - 1) / k, computed with a long. The loop ends on the minimum feasible speed.
Alternatives A linear scan of k is correct but too slow. You cannot start from a formula for k, because the ceiling hours are not a closed form the interviewer expects you to invert.
Java solution
public class KokoEatingBananas {
static int minEatingSpeed(int[] piles, int h) {
int lo = 1, hi = 0;
for (int p : piles) hi = Math.max(hi, p);
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (hoursNeeded(piles, mid) <= h) hi = mid;
else lo = mid + 1;
}
return lo;
}
static long hoursNeeded(int[] piles, int speed) {
long hours = 0;
for (int p : piles) hours += (p + speed - 1L) / speed;
return hours;
}
public static void main(String[] args) {
System.out.println(minEatingSpeed(new int[] {3, 6, 7, 11}, 8)); // 4
System.out.println(minEatingSpeed(new int[] {30, 11, 23, 4, 20}, 5)); // 30
System.out.println(minEatingSpeed(new int[] {30, 11, 23, 4, 20}, 6)); // 23
}
}Complexity O(n log M) time, where M is the largest pile, and O(1) extra space.
Edge cases
- Official samples: piles [3,6,7,11] with h = 8 returns 4; [30,11,23,4,20] with h = 5 returns 30, and with h = 6 returns 23.
- h equals the number of piles: k must be max(pile), because she can give each pile only one hour.
Follow-ups
- Capacity To Ship Packages Within D Days is the same search, with a day's load in place of an hour's bananas.
- Minimum Time Required, in the HackerRank kit, is the same idea on machines that produce items.
Tests [3,6,7,11] with h=8 returns 4. [30,11,23,4,20] with h=5 returns 30, and with h=6 returns 23.
How to say it in the interview "The question is the minimum eating speed that still finishes in h hours. Hours needed falls as speed rises, so I binary-search the speed and, for each mid, I add up the ceiling of each pile divided by mid. The first speed that fits is the answer."
Sources (1)
- LeetCode 875: Koko Eating Bananasleetcode.com