Back to Binary Search
Course Practice
Medium

Maximum Candies Allocated to K Children

LAB

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
5

FUNCTION SHAPE

candies: intArrayk: intint
SOLUTION 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 left
TimeO(n log(max(candies)))
SpaceO(1)
Open on LeetCode
00:00
2 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.