Koko Eating Bananas
Medium Binary Search AI round
piles lists piles of bananas, pile i holding piles[i] of them, and h is a number of hours, at least as many as there are piles.
At a speed of k bananas an hour the piles are eaten one at a time, and no hour is shared between two piles: a pile of p bananas takes p / k hours, rounded up to a whole hour.
Return the smallest whole speed k at which every pile is eaten within h hours.
Example 1
Input: piles = [9,4,13,6], h = 7
Output: 6
At 6 bananas an hour the piles take 2, 1, 3 and 1 hours, 7 in all. At 5 an hour they would take 2, 1, 3 and 2, which is 8, one hour too many.
Example 2
Input: piles = [25,8,17], h = 3
Output: 25
Example 3
Input: piles = [5,5,5,5], h = 20
Output: 1
Constraints
1 ≤ piles.length ≤ 10⁴1 ≤ piles[i] ≤ 10⁹piles.length ≤ h ≤ 10⁹
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Binary Search
- Median of Two Sorted ArraysAI round · Hard
- Search in Rotated Sorted ArrayAI round · Medium
- Find First and Last Position of Element in Sorted ArrayAI round · Medium
- Search Insert PositionEasy
- Search a 2D MatrixMedium