Medium
LABMaximum Candies Allocated to K Children
Given candy piles, return the maximum equal candies each of k children can receive after splitting piles.
EXAMPLES
Example 1
Input
{
"candies": [
5,
8,
6
],
"k": 3
}
Output
5FUNCTION SHAPE
candies: intArrayk: int→intSOLUTION NOTE
Max template. Same pattern as Koko but want maximum instead of minimum.
Reveal reference solution +
pythonREFERENCE
def maximumCandies(self, candies: List[int], k: int) -> int:
def check(size):
return sum(c // size for c in candies) >= k
left, right = 0, max(candies)
while left < right:
mid = ceil(left + (right - left) / 2)
if check(mid):
left = mid
else:
right = mid - 1
return leftTime
O(n log(max(candies)))Space
O(1)