Koko Eating Bananas (Binary Search on Answer)
mediumBinary Search+20 XPTime limit 2s
A monkey has n piles of bananas and h hours. Each hour she picks one pile and eats up to k bananas from it (if the pile has fewer than k, she finishes it and waits for the next hour). Find the smallest whole number k that lets her finish every pile within h hours.
Eating a pile of p bananas at speed k takes ⌈p / k⌉ hours.
Input:
- line 1:
n h - line 2: the
npile sizes
Output: the minimum speed k.
Constraints: 1 ≤ n ≤ 10⁴, n ≤ h ≤ 10⁹, 1 ≤ pile ≤ 10⁹.
Think about it: if speed k works, every faster speed works too. That monotone yes/no lets you binary search on the answer between 1 and the biggest pile.
Sample input 1
4 8 3 6 7 11
Sample output 1
4
Sample input 2
5 5 30 11 23 4 20
Sample output 2
30
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).